<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"mixley.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="全栈·架构设计·算法">
<meta property="og:type" content="website">
<meta property="og:title" content="mixley">
<meta property="og:url" content="https://mixley.github.io/page/3/index.html">
<meta property="og:site_name" content="mixley">
<meta property="og:description" content="全栈·架构设计·算法">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="mixley">
<meta property="article:tag" content="mixley,前端,后端,全栈,收集,java,人脸识别,架构设计,spring,aop,spring boot,spring cloud,深度学习,vue,杂七杂八">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://mixley.github.io/page/3/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>mixley</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?c36eb18366b436984557126f16796f30";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">mixley</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">求上者得中·求中者得下</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2022/3FH16VE.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2022/3FH16VE.html" class="post-title-link" itemprop="url">Undertow</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-06-12 14:35:17" itemprop="dateCreated datePublished" datetime="2022-06-12T14:35:17+08:00">2022-06-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-22 15:31:52" itemprop="dateModified" datetime="2022-04-22T15:31:52+08:00">2022-04-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2022/3FH16VE.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2022/3FH16VE.html" class="cy_cmt_count" data-xid="2022/3FH16VE.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/javastack/p/13552577.html">https://www.cnblogs.com/javastack/p/13552577.html</a></p>
<h1 id="吊打-Tomcat-，Undertow-性能很炸！！"><a href="#吊打-Tomcat-，Undertow-性能很炸！！" class="headerlink" title="吊打 Tomcat ，Undertow 性能很炸！！ "></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/javastack/p/13552577.html">吊打 Tomcat ，Undertow 性能很炸！！ </a></h1><p>在 Java Web 容器的世界里，Tomcat 和 Jetty 是大名鼎鼎的、用的最多的开源项目，也是大众熟知的。</p>
<p>今天再介绍另外一款能和 Tomcat 媲美的神器：Undertow，据说性能方面还要吊打 Tomcat，如果你还不知道它，那你就 OUT 了。</p>
<p>我们来看下 Spring Boot 默认支持的三种 Servlet 容器：</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Servlet Version</th>
</tr>
</thead>
<tbody><tr>
<td>Tomcat 9.0</td>
<td>4.0</td>
</tr>
<tr>
<td>Jetty 9.4</td>
<td>3.1</td>
</tr>
<tr>
<td>Undertow 2.0</td>
<td>4.0</td>
</tr>
</tbody></table>
<p>以上来源于 Spring Boot 2.3.2 官方文档，更多信息请点击<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/YfPFl1_l2sp9YirTppzRow">这里</a>参考这篇文章。</p>
<p>Undertow 它能成为 Spring Boot 默认集成的三大容器之一，就凭这点，我想就足以说明它的地位。</p>
<h2 id="Undertow-什么鬼？"><a href="#Undertow-什么鬼？" class="headerlink" title="Undertow 什么鬼？"></a>Undertow 什么鬼？</h2><p>Undertow 是 RedHat(红帽公司)的开源产品，采用 Java 开发，是一款灵活、高性能的 Web 服务器，提供了基于 NIO 的阻塞/非阻塞 APIs，也是 Wildfly 的默认 Web 容器。</p>
<p>搜索 Undertow：</p>
<p><img src="/2022/3FH16VE/1218593-20200824105305699-195814623.png" alt="img"></p>
<p>页面显示的是 JBoss Community，因为 2006 年 RedHat 收购了 JBoss，那也就不足为怪了。</p>
<p>Undertow 它是一个基于组合的体系结构，可以通过组合一系列小型处理器来构建一个 Web 服务器。这就让我们可以灵活的在 Java EE servlet 4.0 容器和底层非阻塞处理器或者其他更多之间进行选择。</p>
<p>Undertow 被设计成完全可嵌入式的，所以也叫嵌入式容器，具有易于使用的流畅构建 API，另外，Undertow 的生命周期也完全由所嵌入的应用程序所控制。</p>
<p>这也是为什么 Spring Boot 可以直接嵌入 Undertow 的原因，Undertow 它就是为了嵌入而发烧的。Spring Boot 基础知识就不介绍了，关注公众号Java技术栈在后台回复boot获取我写的系列教程。</p>
<p>官方网站：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://undertow.io/">https://undertow.io/</a></p>
</blockquote>
<p>源码托管在 Github：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/undertow-io/undertow">https://github.com/undertow-io/undertow</a></p>
</blockquote>
<h2 id="Undertow-有啥特性？"><a href="#Undertow-有啥特性？" class="headerlink" title="Undertow 有啥特性？"></a>Undertow 有啥特性？</h2><p><strong>1）HTTP/2 Support</strong></p>
<p>Undertow 支持 HTTP/2 开箱即用，不需要重写引导类路径。</p>
<p><strong>2）支持 HTTP 升级</strong></p>
<p>支持 HTTP 升级，允许多个协议通过 HTTP 端口上进行复用。</p>
<p><strong>3）支持 Web Socket</strong></p>
<p>Undertow 提供对 Web 套接字的全面支持，包括对 JSR-356 的支持。</p>
<p><strong>4）支持 Servlet 4.0</strong></p>
<p>Undertow 提供了对 Servlet 4.0 的支持，包括对嵌入式 Servlet 的支持，还可以混合部署 Servlet 和原生 Undertow 非阻塞处理程序。</p>
<p><strong>5）可嵌入式</strong></p>
<p>Undertow 可以嵌入到应用程序中，也可以通过几行代码独立运行。</p>
<p><strong>6）高灵活性</strong></p>
<p>一个 Undertow 服务器是通过链式处理器来配置的，可以根据需要添加功能，因此可以避免添加没有必要的功能。</p>
<h2 id="Undertow-性能如何？"><a href="#Undertow-性能如何？" class="headerlink" title="Undertow 性能如何？"></a>Undertow 性能如何？</h2><p>国外有篇帖子做了 Tomcat vs. Jetty vs. Undertow 三者的性能比较：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/">https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/</a></p>
</blockquote>
<p><img src="/2022/3FH16VE/1218593-20200824105306098-1526118676.png" alt="img"></p>
<p>从测试结果看，这三个 Servlet 容器都具有不错的性能，但 Undertow 性能更好，Tomcat 和 Jetty 紧随其后。</p>
<p>Jetty 在启动时的内存占用最大，为：311 MB, Tomcat 和 Undertow 的初始内存占用都很低，大约为：120 MB，而 Undertow 的初始内存占用最低，为：114 MB。</p>
<p>最后，关键的区别在于，Undertow 响应头参数默认包含 HTTP 持久连接信息，这个头参数在支持持久连接的客户端时，可以通过重用连接来优化性能。</p>
<h2 id="Show-me-the-code"><a href="#Show-me-the-code" class="headerlink" title="Show me the code"></a>Show me the code</h2><p>Undertow 目前有两个主要版本：</p>
<ul>
<li>2.1：当前支持 Servlet 4.0, JDK8+ 的稳定版本；</li>
<li>1.4：当前支持 Servlet 3.1, JDK7 的稳定版本；</li>
</ul>
<p>独立使用 Undertow 需要添加以下依赖：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-servlet<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-websockets-jsr<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>以下示例来源官网：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldServer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(<span class="keyword">final</span> String[] args)</span> </span>&#123;</span><br><span class="line">        Undertow server = Undertow.builder()</span><br><span class="line">                .addHttpListener(<span class="number">8080</span>, <span class="string">&quot;localhost&quot;</span>)</span><br><span class="line">                .setHandler(<span class="keyword">new</span> HttpHandler() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleRequest</span><span class="params">(<span class="keyword">final</span> HttpServerExchange exchange)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, <span class="string">&quot;text/plain&quot;</span>);</span><br><span class="line">                        exchange.getResponseSender().send(<span class="string">&quot;Hello World&quot;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).build();</span><br><span class="line">            server.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这是一个使用了异常 IO 的简单 Hello World 示例。</p>
<h2 id="Spring-Boot-amp-Undertow"><a href="#Spring-Boot-amp-Undertow" class="headerlink" title="Spring Boot &amp; Undertow"></a>Spring Boot &amp; Undertow</h2><p>上面讲到，Undertow 是为嵌入式而生的 Web 容器，又是 Spring Boot 默认集成的容器之一，下面栈长带大家来看下如何在 Spring Boot 中使用 Undertow。</p>
<p>因为在 <code>spring-boot-starter-web</code> 启动器中，Tomcat 是 Spring Boot 默认的嵌入式容器，即：<code>spring-boot-starter-tomcat</code>。</p>
<p>Spring Boot 还提供了其他两个启动器以方便进行代替：</p>
<ul>
<li>spring-boot-starter-jetty</li>
<li>spring-boot-starter-undertow</li>
</ul>
<p>下面来简单实战下，如何在 Spring Boot 中使用 Undertow。Spring Boot 基础知识就不介绍了，不熟悉的可以关注公众号Java技术栈在后台回复boot获取我写的系列教程。</p>
<p>排除 Tomcat 依赖，然后加入 Undertow 依赖：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- Exclude the Tomcat dependency --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-tomcat<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- Use Undertow instead --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-undertow<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>一步就完成集成了，当然实际情况还需要在 application 配置文件中加入 Undertow 的更多自定义或者优化配置参数。</p>
<p>Undertow容器的具体配置可以看这两个类：</p>
<ul>
<li>org.springframework.boot.autoconfigure.web.ServerProperties</li>
<li>org.springframework.boot.autoconfigure.web.ServerProperties.Undertow</li>
</ul>
<p>也可以看 Spring Boot 官方文档：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#server-properties">https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#server-properties</a></p>
</blockquote>
<p>上面有所有 Server 配置参数和说明。</p>
<p>再写一个测试方法测试下：</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.GetMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 微信公众号：Java技术栈</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="built_in">void</span> <span class="function"><span class="title">main</span>(<span class="params"><span class="built_in">String</span>[] args</span>)</span> &#123;</span><br><span class="line">        SpringApplication.run(Application.class);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping</span>(value = <span class="string">&quot;/undertow/test&quot;</span>)</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">String</span> <span class="function"><span class="title">undertow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;hello undertow&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>启动 Application：</p>
<p><img src="/2022/3FH16VE/1218593-20200824105306581-130064430.png" alt="img"></p>
<p>如上所示，可以看到 Undertow 的启动日志。</p>
<p>访问测试方法：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/undertow/test">http://localhost:8080/undertow/test</a></p>
</blockquote>
<p><img src="/2022/3FH16VE/1218593-20200824105306889-830058720.png" alt="img"></p>
<p>输出成功，测试完成。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文对 Undertow 作了一个介绍和集成实战，虽然 Undertow 性能很炸，但你可以去网上找一圈，根本没啥学习资料。</p>
<p>所以，对于学习和一般应用来说，Tomcat 足矣，一方面 Tomcat 学习资料多，另一方面 Tomcat 用的更广泛，很多坑别人帮你踩了，很多漏洞也已经暴露出来了。</p>
<p>那于那些一定要追求极致性能的又不想优化 Tomcat 的，可以考虑使用 Undertow，但同时你要有能力 Hold 住它，需要一定的积累经验，不然出一个问题你线上卡半天显然是不愿意看到的。</p>
<p>最后，网上很多文章说干掉 Tomcat 而要使用 Undertow 的，这就有点夸张，我只能呵呵了，持有保留意见，用啥都行，关键能不能用好。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2022/1M55EP7.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2022/1M55EP7.html" class="post-title-link" itemprop="url">Netflix</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-06-12 14:35:17" itemprop="dateCreated datePublished" datetime="2022-06-12T14:35:17+08:00">2022-06-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 22:10:56" itemprop="dateModified" datetime="2021-11-03T22:10:56+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2022/1M55EP7.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2022/1M55EP7.html" class="cy_cmt_count" data-xid="2022/1M55EP7.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h4 id="Netflix组件替代方案"><a href="#Netflix组件替代方案" class="headerlink" title="Netflix组件替代方案"></a>Netflix组件替代方案</h4><p>Spring Cloud既然把Netflix OSS套件大刀阔斧的砍掉了，那总归得有替代方案吧。那是必然的，Spring Cloud团队给我们推荐了用于替代的产品：</p>
<table>
<thead>
<tr>
<th>Netflix</th>
<th>推荐替代品</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>Hystrix</td>
<td>Resilience4j</td>
<td>Hystrix自己也推荐你使用它代替自己</td>
</tr>
<tr>
<td>Hystrix Dashboard / Turbine</td>
<td>Micrometer + Monitoring System</td>
<td>说白了，监控这件事交给更专业的组件去做</td>
</tr>
<tr>
<td>Ribbon</td>
<td>Spring Cloud Loadbalancer</td>
<td>忍不住了，Spring终究亲自出手</td>
</tr>
<tr>
<td>Zuul 1</td>
<td>Spring Cloud Gateway</td>
<td>忍不住了，Spring终究亲自出手</td>
</tr>
<tr>
<td>Archaius 1</td>
<td>Spring Boot外部化配置 + Spring Cloud配置</td>
<td>比Netflix实现的更好、更强大</td>
</tr>
</tbody></table>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/18A0XWM.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/18A0XWM.html" class="post-title-link" itemprop="url">书籍购买推荐(持续更新)</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-17 22:29:41" itemprop="dateCreated datePublished" datetime="2021-07-17T22:29:41+08:00">2021-07-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-07-22 20:59:16" itemprop="dateModified" datetime="2021-07-22T20:59:16+08:00">2021-07-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/18A0XWM.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/18A0XWM.html" class="cy_cmt_count" data-xid="2021/18A0XWM.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="开发"><a href="#开发" class="headerlink" title="开发"></a>开发</h2><h3 id="神经网络"><a href="#神经网络" class="headerlink" title="神经网络"></a>神经网络</h3><h4 id="入门"><a href="#入门" class="headerlink" title="入门"></a>入门</h4><p>python 神经网络编程——非常适合入门神经网络编程的一本书，初中生就能看懂（微量代码，原理通透）。</p>
<h3 id="架构"><a href="#架构" class="headerlink" title="架构"></a>架构</h3><p>从零开始学架构——由浅入深，有经验的程序员和才开始入行的架构师能收获很多，具体实践中能避免踩坑和注意到一些关键事项</p>
<h3 id="Linux"><a href="#Linux" class="headerlink" title="Linux"></a>Linux</h3><p>Linux编程与Shell脚本编程大全——比较全面详细的linux工具书</p>
<h2 id="建筑"><a href="#建筑" class="headerlink" title="建筑"></a>建筑</h2><h3 id="装修"><a href="#装修" class="headerlink" title="装修"></a>装修</h3><p>小家越住越大——漫画教人搞装修</p>
<h2 id="闲暇阅读"><a href="#闲暇阅读" class="headerlink" title="闲暇阅读"></a>闲暇阅读</h2><h3 id="漫画"><a href="#漫画" class="headerlink" title="漫画"></a>漫画</h3><p>今天你真好看</p>
<p>一禅小和尚</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/11VPC9D.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/11VPC9D.html" class="post-title-link" itemprop="url">学习交流(持续更新)</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-15 22:56:10" itemprop="dateCreated datePublished" datetime="2021-07-15T22:56:10+08:00">2021-07-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-07-27 00:10:00" itemprop="dateModified" datetime="2021-07-27T00:10:00+08:00">2021-07-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/11VPC9D.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/11VPC9D.html" class="cy_cmt_count" data-xid="2021/11VPC9D.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="软件下载专区"><a href="#软件下载专区" class="headerlink" title="软件下载专区"></a>软件下载专区</h2><p><a target="_blank" rel="noopener" href="https://mixley.stdcdn.com/tools.html">常用工具/精品软件</a></p>
<h2 id="书籍购买专区"><a href="#书籍购买专区" class="headerlink" title="书籍购买专区"></a>书籍购买专区</h2><p>[书籍购买推荐】(<a target="_blank" rel="noopener" href="https://mixley.stdcdn.com/book.html">https://mixley.stdcdn.com/book.html</a>)</p>
<h2 id="影视文学"><a href="#影视文学" class="headerlink" title="影视文学"></a>影视文学</h2><h3 id="电影"><a href="#电影" class="headerlink" title="电影"></a>电影</h3><p><a target="_blank" rel="noopener" href="https://tv.cctv.com/2021/02/02/VIDAJNNHiuXByYkLTOs0zo2k210202.shtml">觉醒年代</a>——明白当代青年的使命</p>
<h2 id="代码编程"><a href="#代码编程" class="headerlink" title="代码编程"></a>代码编程</h2><h3 id="教程网站"><a href="#教程网站" class="headerlink" title="教程网站"></a>教程网站</h3><p><a target="_blank" rel="noopener" href="https://www.runoob.com/">菜鸟教程</a>——基础学习必备</p>
<p><a target="_blank" rel="noopener" href="https://www.yiibai.com/">易百教程</a>——基础学习必备</p>
<p><a target="_blank" rel="noopener" href="https://learnku.com/">LearnKu</a>——终身编程</p>
<h2 id="spring-java"><a href="#spring-java" class="headerlink" title="spring/java"></a>spring/java</h2><p><a target="_blank" rel="noopener" href="http://www.spring4all.com/">Spring For All</a> ——民间社区</p>
<p><a target="_blank" rel="noopener" href="https://github.com/wuyouzhuguli/SpringAll">SpringAll</a>——开源项目，由浅入深Spring系列学习</p>
<h3 id="web端"><a href="#web端" class="headerlink" title="web端"></a>web端</h3><p><a target="_blank" rel="noopener" href="http://www.ydma.cn/">兄弟连</a>——H5</p>
<p><a target="_blank" rel="noopener" href="http://www.w3cschool.cn/index-4.html">W3C</a>——web前端</p>
<p><a target="_blank" rel="noopener" href="http://www.w3school.com.cn/index.html">w3</a>——H5</p>
<p><a target="_blank" rel="noopener" href="http://www.axure.com.cn/">Axure中文学习网</a>——快速原型Axure学习</p>
<p><a target="_blank" rel="noopener" href="https://waylau.gitbooks.io/netty-4-user-guide/content/">Netty 4.x</a>——Netty中文文档</p>
<p><a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/">阮一峰</a>——web前端 yyds</p>
<h3 id="论坛-博客平台"><a href="#论坛-博客平台" class="headerlink" title="论坛/博客平台"></a>论坛/博客平台</h3><p><a target="_blank" rel="noopener" href="https://www.52programer.com/">吾爱程序猿</a></p>
<p><a target="_blank" rel="noopener" href="https://www.csdn.net/">csdn</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/">博客园</a></p>
<h3 id="php相关"><a href="#php相关" class="headerlink" title="php相关"></a>php相关</h3><p><a target="_blank" rel="noopener" href="http://www.thinkphp.cn/">ThinkPHP</a></p>
<p><a target="_blank" rel="noopener" href="http://www.golaravel.com/">Laravel(PHP)</a></p>
<h3 id="数据可视化"><a href="#数据可视化" class="headerlink" title="数据可视化"></a>数据可视化</h3><p><a target="_blank" rel="noopener" href="http://www.hcharts.cn/">Highcharts中文网</a></p>
<h2 id="设计相关"><a href="#设计相关" class="headerlink" title="设计相关"></a>设计相关</h2><p><a target="_blank" rel="noopener" href="https://icomoon.io/">ico</a></p>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/I2f4NYE13xLvnsxwDDnWrw">免费素材合集</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1A4411M729">PS2019从入门到精通</a>——从安装到卸载</p>
<h2 id="英语学习"><a href="#英语学习" class="headerlink" title="英语学习"></a>英语学习</h2><p><a target="_blank" rel="noopener" href="https://v.youku.com/v_show/id_XNTgwODM0MTYw.html?spm=a2h1n.8251843.0.0&f=19476545">《跟我学》(Follow Me)BBC 视频</a></p>
<h2 id="公务员"><a href="#公务员" class="headerlink" title="公务员"></a>公务员</h2><p><a target="_blank" rel="noopener" href="https://pan.baidu.com/s/1_qpcc2hMFgdab7oO8gfgzA">公考备考资料</a>——度盘 提取码: 4but 考公资料分享 含思维导图 </p>
<p><a target="_blank" rel="noopener" href="https://m.xue.huatu.com/fx/zx/detail/97845?t=LIAcEr9QHEbxm8rqpoHBJg==">华图在线常识-口诀歌系列</a>——粉笔常识课 王鹏讲得很有趣，可以听听。听完再配合李梦娇的魔性歌曲。一个是帮助理解，一个是加深记忆。两个都学是最好的</p>
<p><a target="_blank" rel="noopener" href="https://pan.baidu.com/s/1OQpccl3AdRRoHgeWsr5mtA">公务员粉笔</a>——度盘 提取码: s3wd  系列教程，较全面 分模块攻略，总结出每道题型的正确答题思路，并固化这种答题思路（划重点）</p>
<h2 id="事业编"><a href="#事业编" class="headerlink" title="事业编"></a>事业编</h2><p><a target="_blank" rel="noopener" href="http://www.fenbi.com/spa/doc-user/dir/4910">事业编—公基资料</a>——事业编资料(打不开多试几次)</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/3VEA8ZS.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/3VEA8ZS.html" class="post-title-link" itemprop="url">win安装centos</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-07-15 20:16:15 / 修改时间：20:16:41" itemprop="dateCreated datePublished" datetime="2021-07-15T20:16:15+08:00">2021-07-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/win/" itemprop="url" rel="index"><span itemprop="name">win</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/3VEA8ZS.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/3VEA8ZS.html" class="cy_cmt_count" data-xid="2021/3VEA8ZS.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="子系统开启"><a href="#子系统开启" class="headerlink" title="子系统开启"></a>子系统开启</h2><p>启用“适用于 Linux 的 Windows 子系统”可选功能</p>
<figure class="highlight powershell"><table><tr><td class="code"><pre><span class="line">dism.exe /online /<span class="built_in">enable-feature</span> /featurename:Microsoft<span class="literal">-Windows</span><span class="literal">-Subsystem</span><span class="literal">-Linux</span> /all /norestart</span><br></pre></td></tr></table></figure>

<p>启用“虚拟机平台”可选功能。 以管理员身份打开 PowerShell 并运行（请保证开启了bios的虚拟化，失败则跳过）：</p>
<figure class="highlight powershell"><table><tr><td class="code"><pre><span class="line">dism.exe /online /<span class="built_in">enable-feature</span> /featurename:VirtualMachinePlatform /all /norestart</span><br></pre></td></tr></table></figure>

<p>若上一个失败，则尝试安装Hyper-V（失败则跳过）。</p>
<figure class="highlight powershell"><table><tr><td class="code"><pre><span class="line"><span class="comment">## 启用Hyper-V</span></span><br><span class="line">DISM /Online /<span class="built_in">Enable-Feature</span> /All /FeatureName:Microsoft<span class="literal">-Hyper</span><span class="literal">-V</span> /all /norestart</span><br></pre></td></tr></table></figure>
<p>下载更新安装wsl2，并设置wsl2为默认（建议重启一次，安装wsl2再使用下面命令）</p>
<figure class="highlight powershell"><table><tr><td class="code"><pre><span class="line"><span class="comment">#下载安装升级wsl2 ，下载链接：https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi</span></span><br><span class="line">wsl -<span class="literal">-set</span><span class="literal">-default</span><span class="literal">-version</span> <span class="number">2</span></span><br></pre></td></tr></table></figure>

<p> 参考链接：</p>
<p>Winux之路-WSL 2的使用及填坑:<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/224753478">https://zhuanlan.zhihu.com/p/224753478</a> </p>
<p>WSL Linux 子系统，真香！完整实操:<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/146545159">https://zhuanlan.zhihu.com/p/146545159</a></p>
<p>【微软】适用于 Linux 的 Windows 子系统安装指南 (Windows 10):<a target="_blank" rel="noopener" href="https://docs.microsoft.com/zh-cn/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package">https://docs.microsoft.com/zh-cn/windows/wsl/install-win10#step-4—download-the-linux-kernel-update-package</a></p>
<h2 id="Centos安装"><a href="#Centos安装" class="headerlink" title="Centos安装"></a>Centos安装</h2><p>下载：<a target="_blank" rel="noopener" href="https://github.com/wsldl-pg/CentWSL">https://github.com/wsldl-pg/CentWSL</a></p>
<p>解压后直接管理员运行</p>
<p><img src="https://mixley.stdcdn.com/2021/07/fb9318d4db326992d8e2e0c37ee55d57.png" alt="image-20210713230538373"></p>
<p>安装成功后再次右击，管理员运行，则直接进入子系统</p>
<p><img src="https://mixley.stdcdn.com/2021/07/513a21ed979f4769369490caca1e805e.png" alt="image-20210713231027745"></p>
<p>参考链接：</p>
<p>在Windows 10上使用WSL安装Centos：<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/272735470">https://zhuanlan.zhihu.com/p/272735470</a></p>
<h2 id="其它操作"><a href="#其它操作" class="headerlink" title="其它操作"></a>其它操作</h2><p>wsl使用</p>
<p>su密码修改：</p>
<figure class="highlight powershell"><table><tr><td class="code"><pre><span class="line">sudo passwd </span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/39Z4WNX.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/39Z4WNX.html" class="post-title-link" itemprop="url">nginx配置文件说明</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-15 20:16:09" itemprop="dateCreated datePublished" datetime="2021-07-15T20:16:09+08:00">2021-07-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/server/" itemprop="url" rel="index"><span itemprop="name">server</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/39Z4WNX.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/39Z4WNX.html" class="cy_cmt_count" data-xid="2021/39Z4WNX.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p> nginx 配置文件说明</p>
<h2 id="基本配置"><a href="#基本配置" class="headerlink" title="基本配置"></a>基本配置</h2><figure class="highlight nginx"><table><tr><td class="code"><pre><span class="line"><span class="attribute">root</span> /home/newland/soft/scenic/dist;</span><br></pre></td></tr></table></figure>

<h2 id="SSL及证书配置"><a href="#SSL及证书配置" class="headerlink" title="SSL及证书配置"></a>SSL及证书配置</h2><figure class="highlight nginx"><table><tr><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">    <span class="comment">#SSL 访问端口号为 443</span></span><br><span class="line">	<span class="attribute">listen</span> <span class="number">443</span> ssl http2;</span><br><span class="line">    <span class="comment">#填写绑定证书的域名</span></span><br><span class="line">    <span class="attribute">server_name</span> test.tencent.cn;</span><br><span class="line">    <span class="attribute">root</span> /test/deploy/dist;</span><br><span class="line">    <span class="comment">#HTTP_TO_HTTPS_END</span></span><br><span class="line">    <span class="attribute">ssl_certificate</span>    /test/fullchain.pem;</span><br><span class="line">    <span class="attribute">ssl_certificate_key</span>    /test/privkey.pem;</span><br><span class="line">    <span class="attribute">ssl_session_timeout</span> <span class="number">10m</span>;</span><br><span class="line">    <span class="attribute">ssl_session_cache</span> shared:SSL:<span class="number">10m</span>;</span><br><span class="line">    <span class="comment">#请按照以下协议配置</span></span><br><span class="line">    <span class="attribute">ssl_protocols</span> TLSv1.<span class="number">1</span> TLSv1.<span class="number">2</span> TLSv1.<span class="number">3</span>;</span><br><span class="line">    <span class="comment">#请按照以下套件配置，配置加密套件，写法遵循 openssl 标准。</span></span><br><span class="line">    <span class="comment">#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; </span></span><br><span class="line">    <span class="attribute">ssl_ciphers</span> EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;</span><br><span class="line">    <span class="attribute">ssl_prefer_server_ciphers</span> <span class="literal">on</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="配置转发"><a href="#配置转发" class="headerlink" title="配置转发"></a>配置转发</h2><figure class="highlight nginx"><table><tr><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">   <span class="comment">#转发方式为连同路径一起转发，如路径输入 /auth时，转发到下面9999的 /auth上</span></span><br><span class="line">   <span class="attribute">location</span> <span class="regexp">~* ^/(auth|code|admin|gen|wxmp|weixin|mall|scenic|sceapi)</span> &#123;</span><br><span class="line">         <span class="attribute">proxy_pass</span> http://127.0.0.1:9999;</span><br><span class="line">         <span class="comment">#proxy_set_header Host $http_host;</span></span><br><span class="line">         <span class="attribute">proxy_connect_timeout</span> <span class="number">15s</span>;</span><br><span class="line">         <span class="attribute">proxy_send_timeout</span> <span class="number">15s</span>;</span><br><span class="line">         <span class="attribute">proxy_read_timeout</span> <span class="number">15s</span>;</span><br><span class="line">         <span class="attribute">proxy_set_header</span> X-Real-IP $remote_addr;</span><br><span class="line">         <span class="attribute">proxy_set_header</span> X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#二级路径转发，当输入 /rental/ 时转发到指定路径</span></span><br><span class="line">    <span class="attribute">location</span> /rental/ &#123;</span><br><span class="line">        <span class="attribute">proxy_pass</span> http://127.0.0.1:8383/;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/2M37JZD.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/2M37JZD.html" class="post-title-link" itemprop="url">yum多服务迁移</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-07-15 20:14:25 / 修改时间：20:14:26" itemprop="dateCreated datePublished" datetime="2021-07-15T20:14:25+08:00">2021-07-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/2M37JZD.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/2M37JZD.html" class="cy_cmt_count" data-xid="2021/2M37JZD.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p> 1、尽量保证服务器版本一致</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">建立yum的缓存包路径，名字任意。</span></span><br><span class="line">mkdir /yumlib</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">编辑yum配置</span></span><br><span class="line">vi /etc/yum.conf</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">录入下面参数(开启缓存，y)</span></span><br><span class="line">keepcache=1</span><br><span class="line">cachedir=/yumlib</span><br></pre></td></tr></table></figure>

<p>2、在其它服务复制/yumlib包，相同位置，并且使用相同配置。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/16ESPVN.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/16ESPVN.html" class="post-title-link" itemprop="url">[转]shell命令</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-07-15 20:14:22 / 修改时间：20:14:58" itemprop="dateCreated datePublished" datetime="2021-07-15T20:14:22+08:00">2021-07-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/16ESPVN.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/16ESPVN.html" class="cy_cmt_count" data-xid="2021/16ESPVN.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="源"><a href="#源" class="headerlink" title="源"></a>源</h1><p>本文出自：<a target="_blank" rel="noopener" href="https://blog.csdn.net/shangboerds/article/details/48623533">https://blog.csdn.net/shangboerds/article/details/48623533</a></p>
<p>一大佬总结，膜拜一下！</p>
<h1 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h1><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Hello World.&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>第一行代码称为沙邦(sh-bang)，表示由什么程序运行此脚本。<br>第二行的 print 是一个命令，用来打印输出。</p>
<h1 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h1><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 这里是单行注释，单行注释以 # 开始到行尾。</span></span><br><span class="line"></span><br><span class="line">:&lt;&lt;!</span><br><span class="line">这里是多行注释，! 可以是任何成对的字符</span><br><span class="line">!</span><br></pre></td></tr></table></figure>

<h1 id="命令分隔符"><a href="#命令分隔符" class="headerlink" title="命令分隔符"></a>命令分隔符</h1><p>通常，ksh 认为每一行就是一个命令或语句，如果一行包含多条语句，你可以在语句之间加上分号(;)。当然，你也可以在每一行后面加上分号。</p>
<h1 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h1><h2 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"><span class="comment"># 定义常量方法 1</span></span><br><span class="line"><span class="built_in">typeset</span> -r CONSTANT_INT=110;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义常量方法 2</span></span><br><span class="line"><span class="built_in">readonly</span> CONSTANT_STR=<span class="string">&quot;TEST&quot;</span>;</span><br></pre></td></tr></table></figure>



<h2 id="数值"><a href="#数值" class="headerlink" title="数值"></a>数值</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 定义整数变量</span></span><br><span class="line"><span class="built_in">integer</span> var_int1=10; <span class="comment"># 方法 1(推荐)</span></span><br><span class="line"><span class="built_in">typeset</span> -i var_int2=-10; <span class="comment"># 方法 2</span></span><br><span class="line"><span class="built_in">let</span> <span class="string">&quot;var_int3=20&quot;</span>; <span class="comment"># 方法 3</span></span><br><span class="line">((var_int4=30)); <span class="comment"># 方法4</span></span><br><span class="line"><span class="built_in">integer</span> var_int5=010; <span class="comment"># 八进制以 0 开头</span></span><br><span class="line"><span class="built_in">integer</span> var_int6=0x10; <span class="comment"># 十六进制以 0x 开头</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#--- 2# 表示后面的数字是二进制</span></span><br><span class="line"><span class="comment">#--- B#N B表示进制，最大 64</span></span><br><span class="line"><span class="built_in">integer</span> var_int5=2<span class="comment">#10;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义浮点数变量</span></span><br><span class="line"><span class="built_in">float</span> var_float1=250.0; <span class="comment"># 方法 1(推荐)</span></span><br><span class="line"><span class="built_in">typeset</span> -F var_float2=250.; <span class="comment"># 方法 2</span></span><br><span class="line"><span class="built_in">typeset</span> -F var_float3=0.25;</span><br><span class="line"><span class="built_in">typeset</span> -F var_float4=.25;</span><br><span class="line"><span class="built_in">typeset</span> -F var_float5=-250.0e2; <span class="comment"># -250.0乘以 10 的 2 次方，e不区分大小写</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># typeset -E 用来定义双精度浮点数变量</span></span><br><span class="line"><span class="built_in">typeset</span> -E var_double1=250.0;</span><br><span class="line"><span class="built_in">typeset</span> -E var_double2=250.;</span><br><span class="line"><span class="built_in">typeset</span> -E var_double3=0.25;</span><br><span class="line"><span class="built_in">typeset</span> -E var_double4=.25;</span><br><span class="line"><span class="built_in">typeset</span> -E var_double5=-250.0e2; <span class="comment"># -250.0乘以 10 的 2 次方，e不区分大小写</span></span><br></pre></td></tr></table></figure>



<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 方式1：双引号  </span></span><br><span class="line"><span class="comment"># 双引号内的字符串可以进行变量插值和字符转义  </span></span><br><span class="line"><span class="built_in">typeset</span> str=<span class="string">&quot;Zhang San&quot;</span>;  </span><br><span class="line"><span class="built_in">typeset</span> string=<span class="string">&quot;Li Si&quot;</span>;  </span><br><span class="line"><span class="built_in">typeset</span> text1=<span class="string">&quot;my name is <span class="variable">$str</span>\n&quot;</span>; <span class="comment">#此处的 $str 将被字符串张三替换，这种能力称之为变量插值，\n 为转义字符  </span></span><br><span class="line"><span class="built_in">typeset</span> text2=<span class="string">&quot;my name is <span class="variable">$string</span>\n&quot;</span>; <span class="comment"># 此处的变量到底是 $str 还是 $string 呢？默认是最长匹配，也就是$string  </span></span><br><span class="line"><span class="built_in">typeset</span> text3=<span class="string">&quot;my name is <span class="variable">$&#123;str&#125;</span>ing\n&quot;</span>; <span class="comment"># 如果想插入变量 $str的值，可以将变量名用大括号围起来  </span></span><br><span class="line">  </span><br><span class="line"><span class="comment"># 方式2：单引号  </span></span><br><span class="line"><span class="comment"># 单引号内的任何字符都代表它自己，无法进行变量插值和字符转义  </span></span><br><span class="line"><span class="built_in">typeset</span> text4=<span class="string">&#x27;my name is $str\n&#x27;</span>; <span class="comment">#此处的 $str 无法被替换为张三，\n 也无法代表换行符</span></span><br></pre></td></tr></table></figure>



<h2 id="字符串函数"><a href="#字符串函数" class="headerlink" title="字符串函数"></a>字符串函数</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment">######################### 长度 </span></span><br><span class="line"><span class="comment"># $&#123;#xx&#125; 得到变量 xx 的长度 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;the length of <span class="variable">$str</span> is <span class="variable">$&#123;#str&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">######################### 子串</span></span><br><span class="line"><span class="comment"># $&#123;xx:start&#125; 从 start 开始得到变量 xx 的子串</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Get substring from 11: <span class="variable">$&#123;text1:11&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx:start:length&#125; 从 start 开始,取 length 个字符，得到变量 xx 的子串</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Get substring from 11 and get 5 characters: <span class="variable">$&#123;text1:11：5&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx:start&#125; 和 $&#123;xx:start:length&#125;， 注意 start 可以是负值哦，表示相对于字符串的末尾</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Get substring from -9: <span class="variable">$&#123;text1:-9&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">######################### 替换</span></span><br><span class="line"><span class="comment"># $&#123;xx/pattern/replace&#125; pattern 是个正则表达式，将变量 xx 中 pattern 匹配的字串替换成 replace 指定的字符串，非贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;text1 after replaced Zhang to Wang: <span class="variable">$&#123;text1/Zhang/Wang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx//pattern/replace&#125; 同上，贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;text1 after replaced Zhang to Wang: <span class="variable">$&#123;text1//Zhang/Wang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line"><span class="comment"># $&#123;xx/#pattern/replace&#125; pattern 是个正则表达式，从变量 xx 开头查找 pattern 匹配的字串替，如果找到则换成 replace 指定的字符串，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after replaced Zhang to Wang: <span class="variable">$&#123;str/#Zhang/Wang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx/%pattern/replace&#125; pattern 是个正则表达式，查找变量 xx 是否以 pattern 匹配的字串替结尾，如果找到则换成 replace 指定的字符串，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after replaced San to Four: <span class="variable">$&#123;str/%San/Four&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">######################### 删除子串</span></span><br><span class="line"><span class="comment"># $&#123;xx/pattern&#125; pattern 是个正则表达式，删除变量 xx 中 pattern 匹配的子串，非贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;text1 after removed Zhang: <span class="variable">$&#123;text1/Zhang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line"><span class="comment"># $&#123;xx#pattern&#125; pattern 是个正则表达式，从变量 xx 开头查找 pattern 匹配的字串替，如果找到则删除，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after removed Zhang: <span class="variable">$&#123;str#Zhang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx##pattern&#125; 同上，贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after removed Zhang: <span class="variable">$&#123;str##Zhang&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx%pattern&#125; pattern 是个正则表达式，查找变量 xx 是否以 pattern 匹配的字串替结尾，如果找到则删除，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after removed San: <span class="variable">$&#123;str%San&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx%%pattern&#125; 同上，贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;str after removed San: <span class="variable">$&#123;str%San&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">######################### 判断是否为空</span></span><br><span class="line"><span class="comment"># 接收参数  </span></span><br><span class="line">name=<span class="variable">$1</span>;  </span><br><span class="line">age=<span class="variable">$2</span>;  </span><br><span class="line">gendar=<span class="variable">$3</span>;  </span><br><span class="line">description=<span class="variable">$4</span>;  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># $&#123;xx:-&quot;test&quot;&#125; 表示如果变量 xx 存在且不为 null，返回其值，否则返回  test</span></span><br><span class="line">name=<span class="variable">$&#123;name:-&quot;Shang Bo&quot;&#125;</span>;  </span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;name=<span class="variable">$name</span>&quot;</span>;  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># $&#123;xx:=&#x27;test&#x27;&#125; 表示如果变量 xx 存在且不为 null，返回其值，否则设置 xx 为 test 并返回 test  </span></span><br><span class="line">age=<span class="variable">$&#123;age:=&#x27;30&#x27;&#125;</span>;  </span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;age=<span class="variable">$age</span>&quot;</span>;  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># $&#123;xx:+&#x27;test&#x27;&#125; 表示如果变量 xx 存在且不为 null，返回 test， 否则返回 null  </span></span><br><span class="line">description=<span class="string">&quot;a java developer&quot;</span>;  </span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;<span class="variable">$&#123;description:+&#x27;description already defined&#x27;&#125;</span>&quot;</span>;  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># $&#123;xx:?&quot;error&quot;&#125; 表示如果变量 xx 存在且不为 null，返回其值，否则显示 error 错误消息 并终止程序  </span></span><br><span class="line">gendar=<span class="variable">$&#123;gendar:?&quot;gendar missing.&quot;&#125;</span>;  </span><br><span class="line"></span><br><span class="line"><span class="comment"># -n 判断字符串不为空，长度不为0  </span></span><br><span class="line"><span class="keyword">if</span> [ -n <span class="string">&quot;<span class="variable">$name</span>&quot;</span> ]; <span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$name</span> is not empty **#&quot;</span>  </span><br><span class="line"><span class="keyword">fi</span>  </span><br><span class="line"></span><br><span class="line"><span class="comment"># -z 判断字符串为空.长度为0.  </span></span><br><span class="line"><span class="keyword">if</span> [ -z <span class="string">&quot;<span class="variable">$x</span>&quot;</span> ];<span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> is empty **#&quot;</span>  </span><br><span class="line"><span class="keyword">fi</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">######################### 其他</span></span><br><span class="line"><span class="comment"># $&#123;!xx*&#125; 或 $&#123;!xx@&#125; 得到 以 xx 开头的所有变量</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;We have variables <span class="variable">$&#123;!text*&#125;</span> who start with text&quot;</span>;</span><br><span class="line"></span><br><span class="line">alpha=<span class="string">&quot;   aBcDeFgHiJkLmNoPqRsTuVwXyZ   &quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 去掉前面空格</span></span><br><span class="line"><span class="built_in">typeset</span> -L v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Remove leading spaces: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 去掉尾部空格</span></span><br><span class="line"><span class="built_in">typeset</span> -R v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Remove trailing spaces: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 去掉前面空格后，保留 10 位字符，如果长度不够 10 位则在尾部补空格</span></span><br><span class="line"><span class="built_in">typeset</span> -L10 v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;truncate to 10 after remove leading spaces: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 去掉尾部空格，保留 16 位字符，如果长度不够 16 位则在前面补空格</span></span><br><span class="line"><span class="built_in">typeset</span> -R16 v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;truncate to 16 after remove trailing spaces: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在前面补 0 使整个数字成 8 位</span></span><br><span class="line"><span class="built_in">typeset</span> -Z8 v=<span class="string">&quot;123.50&quot;</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;add leading 0: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 去掉前面的 0 使整个数字成 6 位</span></span><br><span class="line"><span class="built_in">typeset</span> -LZ6 v=<span class="string">&quot;00123.50&quot;</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;strips leading 0: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 全部转成小写</span></span><br><span class="line"><span class="built_in">typeset</span> -l v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;to lowercase: <span class="variable">$v</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 全部转成大写</span></span><br><span class="line"><span class="built_in">typeset</span> -u v=<span class="variable">$alpha</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;to uppercase: <span class="variable">$v</span>&quot;</span>;</span><br></pre></td></tr></table></figure>



<h2 id="转义"><a href="#转义" class="headerlink" title="转义"></a>转义</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 转义字符</span></span><br><span class="line">\a	响铃</span><br><span class="line">\b	退格</span><br><span class="line">\c  终止处理字符串</span><br><span class="line">\E	Esc键</span><br><span class="line">\f	换页</span><br><span class="line">\n	换行</span><br><span class="line">\r	回车</span><br><span class="line">\t	水平制表符</span><br><span class="line">\v	垂直制表符</span><br><span class="line">\0n 表示n为8进制数字</span><br><span class="line">\\	反斜线</span><br></pre></td></tr></table></figure>



<h2 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 定义函数</span></span><br><span class="line"><span class="keyword">function</span> fun_test &#123;</span><br><span class="line">	<span class="comment"># 定义局部变量</span></span><br><span class="line">	<span class="built_in">typeset</span> local_vardefined_in_function=<span class="string">&quot;local variable defined in function&quot;</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment"># 在函数中定义全局变量	</span></span><br><span class="line">	global_var_defined_in_function=<span class="string">&quot;global variable defined in function&quot;</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment"># 改变全局变量的值</span></span><br><span class="line">	global_var_defined_in_main+=<span class="string">&quot; changed in function&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义全局变量</span></span><br><span class="line"><span class="built_in">typeset</span> global_var_defined_in_main=<span class="string">&quot;global variable defined in main&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;global_var_defined_in_main=<span class="variable">$global_var_defined_in_main</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Call function&quot;</span>;</span><br><span class="line">fun_test;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 打印值是否改变</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;global_var_defined_in_main=<span class="variable">$global_var_defined_in_main</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;global_var_defined_in_function=<span class="variable">$global_var_defined_in_function</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;local_vardefined_in_function=<span class="variable">$local_vardefined_in_function</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果</span></span><br><span class="line">global_var_defined_in_main=global variable defined <span class="keyword">in</span> main</span><br><span class="line">Call <span class="keyword">function</span></span><br><span class="line">global_var_defined_in_main=global variable defined <span class="keyword">in</span> main changed <span class="keyword">in</span> <span class="keyword">function</span></span><br><span class="line">global_var_defined_in_function=global variable defined <span class="keyword">in</span> <span class="keyword">function</span></span><br><span class="line">local_vardefined_in_function=</span><br></pre></td></tr></table></figure>

<h2 id="变量导出"><a href="#变量导出" class="headerlink" title="变量导出"></a>变量导出</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 导出变量方法 1</span></span><br><span class="line"><span class="built_in">typeset</span> -x CONSTANT_INT=110;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 导出变量方法 2</span></span><br><span class="line"><span class="built_in">export</span> CONSTANT_STR=<span class="string">&quot;TEST&quot;</span>;</span><br></pre></td></tr></table></figure>



<h1 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h1><p>ksh 的正则表达式和其他语言有一些区别，最重要的区别是，在 ksh 中， ? 代表任意一个字符，* 代表任意多个字符，那么它如何匹配次数呢？看看下面的表格吧</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">操作符	            描述</span><br><span class="line">*(exp)              贪婪匹配，匹配任意次</span><br><span class="line">+(exp)              贪婪匹配，匹配 1次或多次</span><br><span class="line">?(exp)              贪婪匹配，匹配 0或1次</span><br><span class="line">&#123;N&#125;(exp)            贪婪匹配，匹配 N 次</span><br><span class="line">&#123;N,M&#125;(exp)          贪婪匹配，匹配 N 到 M 次</span><br><span class="line">@(exp1|exp2|...)    贪婪匹配，匹配 exp1 或 exp2 ...</span><br><span class="line">*-(exp)             非贪婪匹配，匹配任意次</span><br><span class="line">+-(exp)             非贪婪匹配，匹配 1次或多次</span><br><span class="line">?-(exp)             非贪婪匹配，匹配 0或1次</span><br><span class="line">&#123;N&#125;-(exp)           非贪婪匹配，匹配 N 次</span><br><span class="line">&#123;N,M&#125;-(exp)         非贪婪匹配，匹配 N 到 M 次</span><br><span class="line">@-(exp1|exp2|...)	非贪婪匹配，匹配 exp1 或 exp2 ...</span><br><span class="line">!(exp)              匹配非 exp 的任何东西</span><br><span class="line">[:word:]            匹配字母或下划线</span><br><span class="line">\d                  匹配数字，同[[:digit:]]</span><br><span class="line">\D                  匹配非数字，同[![:digit:]]</span><br><span class="line">\s                  匹配空白字符，同[[:space:]]</span><br><span class="line">\S                  匹配非空白字符，同[![:space:]]</span><br><span class="line">\w                  匹配匹配字母或下划线，同[[:word:]]</span><br><span class="line">\W                  匹配非匹配字母或下划线，同[![:word:]]</span><br></pre></td></tr></table></figure>

<p>下面是使用正则表达式的一个简单例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 匹配</span></span><br><span class="line"><span class="built_in">typeset</span> brithday=<span class="string">&#x27;2013-06-25&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$&#123;brithday&#125;</span> = &#123;4&#125;(\d)-&#123;2&#125;(\d)-&#123;2&#125;(\d) ]]; <span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;legal date&quot;</span>  </span><br><span class="line"><span class="keyword">fi</span>  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 替换</span></span><br><span class="line"><span class="built_in">typeset</span> str=<span class="string">&quot;Zhang yi, zhang er, Zhuang Si, ZHANG WU&quot;</span>;</span><br><span class="line">  </span><br><span class="line"><span class="comment"># $&#123;xx/pattern/replace&#125; pattern 是个正则表达式，将变量 xx 中 pattern 匹配的字串替换成 replace 指定的字符串，非贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx/pattern/replace&#125; : <span class="variable">$&#123;str/Z*(\w)/C\1&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx//pattern/replace&#125; 同上，贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx//pattern/replace&#125; : <span class="variable">$&#123;str//Z*(\w)/C\1&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ~(+i:pattern) 表示 pattern使用忽略大小写匹配模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx//pattern/replace&#125; : <span class="variable">$&#123;str//~(+i:Z*(\w))/C\1&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line"><span class="comment"># $&#123;xx/#pattern/replace&#125; pattern 是个正则表达式，从变量 xx 开头查找 pattern 匹配的字串替，如果找到则换成 replace 指定的字符串，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx/#pattern/replace&#125; : <span class="variable">$&#123;str/#Z*(\w)/C\1&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx/%pattern/replace&#125; pattern 是个正则表达式，查找变量 xx 是否以 pattern 匹配的字串替结尾，如果找到则换成 replace 指定的字符串，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx/%pattern/replace&#125; : <span class="variable">$&#123;str/%?(\w)/*&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################  删除子串</span></span><br><span class="line"><span class="comment"># $&#123;xx/pattern&#125; pattern 是个正则表达式，删除变量 xx 中 pattern 匹配的子串，非贪婪模式 </span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx/pattern&#125; : <span class="variable">$&#123;str/Z*(\w)&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line"><span class="comment"># $&#123;xx#pattern&#125; pattern 是个正则表达式，从变量 xx 开头查找 pattern 匹配的字串替，如果找到则删除，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx#pattern&#125; : <span class="variable">$&#123;str#Z*(\w)&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx##pattern&#125; 同上，贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx##pattern&#125; : <span class="variable">$&#123;str##Z*(\w)&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx%pattern&#125; pattern 是个正则表达式，查找变量 xx 是否以 pattern 匹配的字串替结尾，如果找到则删除，非贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx%pattern&#125; : <span class="variable">$&#123;str%W*(\w)&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $&#123;xx%%pattern&#125; 同上，贪婪模式</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;\$&#123;xx%%pattern&#125; : <span class="variable">$&#123;str%%W*(\w)&#125;</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>上面介绍的正则表达式有个缺陷，它只能匹配英语，如果你想匹配其他语言，你可以使用标准的 POSIX 语法，如下。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">元字符(Metacharacter)	匹配(Matches)</span><br><span class="line">[[:alnum:]]	            字母和数字</span><br><span class="line">[[:alpha:]]	            字母</span><br><span class="line">[[:lower:]]	            小写字母</span><br><span class="line">[[:upper:]]	            大写字母</span><br><span class="line">[[:digit:]]	            数字</span><br><span class="line">[[:blank:]]	            空格和制表符</span><br><span class="line">[[:space:]]	            空白字符</span><br><span class="line">[[:graph:]]	            非空白字符</span><br><span class="line">[[:print:]]	            类似[[:graph:]]，但是包含空白字符</span><br><span class="line">[[:punct:]]	            标点符号</span><br><span class="line">[[:cntrl:]]	            控制字符</span><br><span class="line">[[:xdigit:]]	        十六进制中容许出现的数字(例如 0-9a-fA-f)</span><br><span class="line">[. xx .]                将 xx 作为一个整体匹配, xx 可以是任何字母</span><br><span class="line">[= e =]                 认为等价，在法语中匹配 e, è, 或 é</span><br></pre></td></tr></table></figure>

<h1 id="复合变量"><a href="#复合变量" class="headerlink" title="复合变量"></a>复合变量</h1><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义复合变量 方法 1</span></span><br><span class="line">zhang_san=<span class="string">&quot;Zhang San&quot;</span></span><br><span class="line">zhang_san.firstname=San</span><br><span class="line">zhang_san.lastname=Zhang</span><br><span class="line">zhang_san.age=18</span><br><span class="line"><span class="built_in">print</span> <span class="variable">$zhang_san</span> <span class="comment"># 简单打印</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义复合变量 方法 2，注意=后面的一个空格</span></span><br><span class="line">li_si= (firstname=si lastname=li age=28);</span><br><span class="line"><span class="built_in">print</span> -r <span class="variable">$li_si</span>; <span class="comment"># 复杂打印，可读性更好</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义复合变量 方法 3，注意=后面的一个空格</span></span><br><span class="line">wang_wu= (<span class="built_in">typeset</span> firstname=wu; <span class="built_in">typeset</span> lastname=Wang; <span class="built_in">typeset</span> -i age=38);</span><br><span class="line"><span class="built_in">print</span> -r <span class="variable">$wang_wu</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加变量，注意+=后面的一个空格</span></span><br><span class="line">wang_wu+= (<span class="built_in">typeset</span> gender=man);</span><br><span class="line"><span class="built_in">print</span> -r <span class="variable">$wang_wu</span>;</span><br></pre></td></tr></table></figure>

<h1 id="算术运算符"><a href="#算术运算符" class="headerlink" title="算术运算符"></a>算术运算符</h1><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=2;</span><br><span class="line"><span class="built_in">typeset</span> -i y=3;</span><br><span class="line"><span class="built_in">typeset</span> -i r=0;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 注意，算术表达式需要包含在$(())中，否则成了文本表达式</span></span><br><span class="line"><span class="comment"># 注意，括号中没有 $ 哦</span></span><br><span class="line">r=$((x+y)); <span class="comment">#加</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x+y=<span class="variable">$r</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">r=$((x-y)); <span class="comment">#减</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x-y=<span class="variable">$r</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">r=$((x*y)); <span class="comment">#乘</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x*y=<span class="variable">$r</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">r=$((x/y)); <span class="comment">#除</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x/y=<span class="variable">$r</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">r=$((x**y)); <span class="comment">#幂，相当于2的3次方</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x**y=<span class="variable">$r</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">r=$((x%y)); <span class="comment">#余</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x%y=<span class="variable">$r</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h2 id="数字比较运算符"><a href="#数字比较运算符" class="headerlink" title="数字比较运算符"></a>数字比较运算符</h2><p>比较数字有两种方法。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=20;</span><br><span class="line"><span class="built_in">typeset</span> -i y=3;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 注意，数字比较时需要包含在 （（））中</span></span><br><span class="line"><span class="comment"># 注意，括号中没有 $ 哦</span></span><br><span class="line"><span class="comment"># 大于</span></span><br><span class="line"><span class="keyword">if</span> ((x &gt; y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -gt <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> gt <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 大于等于</span></span><br><span class="line"><span class="keyword">if</span> ((x &gt;= y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt;= <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -ge <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> ge <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 小于</span></span><br><span class="line"><span class="keyword">if</span> ((x &lt; y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -lt <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> lt <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 小于等于</span></span><br><span class="line"><span class="keyword">if</span> ((x &lt;= y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt;= <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -le <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> le <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 等于</span></span><br><span class="line"><span class="keyword">if</span> ((x == y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> == <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -eq <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> le <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 不等于</span></span><br><span class="line"><span class="keyword">if</span> ((x != y)); <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> != <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -ne <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> ne <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h2 id="字符串比较运算符"><a href="#字符串比较运算符" class="headerlink" title="字符串比较运算符"></a>字符串比较运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 支持 [] 和 [[]] 测试条件表达式，注意，它们有一些区别，推荐使用 [[]]</span></span><br><span class="line"><span class="comment"># 变量是否包含在双引号中也有一些区别，推荐不要使用双引号</span></span><br><span class="line"><span class="built_in">typeset</span> x=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"><span class="built_in">typeset</span> y=<span class="string">&#x27;b&#x27;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断字符串不为空</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> is not empty **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断字符串不为空，长度不为0</span></span><br><span class="line"><span class="keyword">if</span> [[ -n <span class="variable">$x</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> is not empty **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断字符串为空.长度为0.</span></span><br><span class="line"><span class="keyword">if</span> [[ -z <span class="variable">$x</span> ]];<span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> is empty **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 等于 -- 精确匹配</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$x</span> = a* ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 1 <span class="variable">$x</span> = a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$x</span> == a* ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 2 <span class="variable">$x</span> == a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 等于 -- 精确匹配</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> = <span class="string">&quot;a*&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 3 \&quot;<span class="variable">$x</span>\&quot; = \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> == <span class="string">&quot;a*&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 4 \&quot;<span class="variable">$x</span>\&quot; == \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 等于 -- 精确匹配</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> = <span class="string">&quot;a*&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 5 \&quot;<span class="variable">$x</span>\&quot; = \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> == <span class="string">&quot;a*&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 6 \&quot;<span class="variable">$x</span>\&quot; == \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 等于 -- 匹配模式</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> = a* ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 7 <span class="variable">$x</span> start with a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> == a* ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 8 <span class="variable">$x</span> start with a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 不等于 -- 精确匹配</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$x</span> != a* ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 1 <span class="variable">$x</span> != a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> != <span class="string">&quot;a*&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 2 \&quot;<span class="variable">$x</span>\&quot; != \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$x</span>&quot;</span> != <span class="string">&quot;a*&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 3 \&quot;<span class="variable">$x</span>\&quot; != \&quot;a*\&quot; **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 不等于 -- 精确模式</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> != a* ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** 4 <span class="variable">$x</span> != a* **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 大于,注意：字符串没有大于等于操作符</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> &gt; <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 小于,注意：字符串没有小于等于操作符</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> &lt; <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> x=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"><span class="built_in">typeset</span> y=<span class="string">&#x27;b&#x27;</span>;</span><br><span class="line"><span class="built_in">typeset</span> z=<span class="string">&#x27;c&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 与</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> &lt; <span class="variable">$y</span> &amp;&amp; <span class="variable">$y</span> &lt; <span class="variable">$z</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt; <span class="variable">$y</span> &lt; <span class="variable">$z</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 或</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> &lt; <span class="variable">$y</span> || <span class="variable">$y</span> &lt; <span class="variable">$z</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt; <span class="variable">$y</span> || <span class="variable">$y</span> &lt; <span class="variable">$z</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 非</span></span><br><span class="line"><span class="keyword">if</span> [[ ! <span class="variable">$x</span> &gt; <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt;= <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h2 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 按位与 &amp;</span></span><br><span class="line"><span class="comment"># 按位或 |</span></span><br><span class="line"><span class="comment"># 按位非 ~</span></span><br><span class="line"><span class="comment"># 按位异或 ^</span></span><br><span class="line"><span class="comment"># 左移(相当于乘2) &lt;&lt;</span></span><br><span class="line"><span class="comment"># 右移(相当于除2) &gt;&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># =</span></span><br><span class="line"><span class="comment"># +=</span></span><br><span class="line"><span class="comment"># -=</span></span><br><span class="line"><span class="comment"># *=</span></span><br><span class="line"><span class="comment"># /=</span></span><br><span class="line"><span class="comment"># %=</span></span><br><span class="line"><span class="comment"># &amp;=</span></span><br><span class="line"><span class="comment"># ^=</span></span><br><span class="line"><span class="comment"># &lt;&lt;= </span></span><br><span class="line"><span class="comment"># &gt;&gt;=</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=2;</span><br><span class="line"><span class="built_in">typeset</span> -i r=0;</span><br><span class="line"></span><br><span class="line"><span class="comment"># (()) 用来计算数学表达式</span></span><br><span class="line">((r+=x));</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;r=<span class="variable">$r</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h2 id="自增自减运算符"><a href="#自增自减运算符" class="headerlink" title="自增自减运算符"></a>自增自减运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=1;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 自增运算符</span></span><br><span class="line">((x++));</span><br><span class="line">((++x));</span><br><span class="line"></span><br><span class="line"><span class="comment"># 自减运算符</span></span><br><span class="line">((x--));</span><br><span class="line">((--x));</span><br></pre></td></tr></table></figure>

<h2 id="逗号运算符"><a href="#逗号运算符" class="headerlink" title="逗号运算符"></a>逗号运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=1;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 逗号表达式</span></span><br><span class="line">((x++,++x));</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;x=<span class="variable">$x</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h2 id="条件运算符"><a href="#条件运算符" class="headerlink" title="条件运算符"></a>条件运算符</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=2;</span><br><span class="line"><span class="built_in">typeset</span> -i y=3;</span><br><span class="line"><span class="built_in">typeset</span> -i r=0;</span><br><span class="line"></span><br><span class="line">((r=(y &gt; x) ? y : x));</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;r=<span class="variable">$r</span>&quot;</span>; </span><br></pre></td></tr></table></figure>

<h1 id="文件测试"><a href="#文件测试" class="headerlink" title="文件测试"></a>文件测试</h1><p>有时候我们需要判断某个文件是否存在，很简单，使用 -e 即可，下面是一个简单例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> test_file=<span class="string">&quot;test.txt&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ ! -e <span class="variable">$test_file</span> ]]; <span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$test_file</span> does not exist&quot;</span>;</span><br><span class="line">	<span class="built_in">exit</span> 2;</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>除此之外，我们还可以通过下面的操作符来测试文件的其他属性。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">文件测试操作符  意义</span><br><span class="line">-e              文件或目录，是否存在</span><br><span class="line">-s              文件存在且不为空</span><br><span class="line">-d              是否为目录</span><br><span class="line">-f              是否为普通文件</span><br><span class="line">-L              是否为符号链接</span><br><span class="line">-r              文件或目录，对当前用户或组来说是可读的</span><br><span class="line">-w              文件或目录，对当前用户或组来说是可写的</span><br><span class="line">-x              文件或目录，对当前用户或组来说是可执行的</span><br><span class="line">-O              文件或目录，当前用户是否是文件的拥有者</span><br><span class="line">-G              文件或目录，当前用户是否和文件属于同一个组</span><br><span class="line">file1 -nt file2 file1 是否比 file2 新</span><br><span class="line">file1 -ot file2 file1 是否比 file2 旧</span><br><span class="line">file1 -ef file2 file1 和 file2 是否是同一个文件</span><br></pre></td></tr></table></figure>

<h1 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h1><h2 id="索引数组（Indexed-Arrays）"><a href="#索引数组（Indexed-Arrays）" class="headerlink" title="索引数组（Indexed Arrays）"></a>索引数组（Indexed Arrays）</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 定义数组</span></span><br><span class="line"><span class="comment"># 方法 1</span></span><br><span class="line">names[0]=<span class="string">&quot;Shang Bo&quot;</span>;</span><br><span class="line">names[1]=<span class="string">&quot;Zhang San&quot;</span>;</span><br><span class="line">names[2]=<span class="string">&quot;Li Si&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法 2</span></span><br><span class="line">names=(<span class="string">&quot;Shang Bo&quot;</span> <span class="string">&quot;Zhang San&quot;</span> <span class="string">&quot;Li Si&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法 3</span></span><br><span class="line"><span class="built_in">set</span> -A names <span class="string">&quot;Shang Bo&quot;</span> <span class="string">&quot;Zhang San&quot;</span> <span class="string">&quot;Li Si&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 访问数组</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The first element of names is <span class="variable">$&#123;names[0]&#125;</span>&quot;</span>; <span class="comment"># 取出数组下标为 0 的元素</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names contains <span class="variable">$&#123;names[*]&#125;</span>&quot;</span>; <span class="comment"># 取出所有元素</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names contains <span class="variable">$&#123;#names[*]&#125;</span> elements&quot;</span>; <span class="comment"># 数组长度</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names contains <span class="variable">$&#123;names[@]&#125;</span>&quot;</span>; <span class="comment"># 取出所有元素</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names contains <span class="variable">$&#123;#names[@]&#125;</span> elements&quot;</span>; <span class="comment"># 数组长度</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 操作数组</span></span><br><span class="line">names+=(<span class="string">&quot;Wang Wu&quot;</span> <span class="string">&quot;Li Qi&quot;</span>); <span class="comment"># 向数组中添加数据</span></span><br><span class="line"></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:1&#125;</span>); <span class="comment"># 数组提取，表示从下标 1的元素开始的所有元素</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_part contains <span class="variable">$&#123;names_part[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:1:2&#125;</span>); <span class="comment"># 数组提取，表示从下标 1的元素开始，取两个元素</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_part contains <span class="variable">$&#123;names_part[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:0&#125;</span>); <span class="comment"># 数组复制</span></span><br><span class="line">names_replace=(<span class="variable">$&#123;names_part[@]/Li/Wang&#125;</span>); <span class="comment"># 数组替换(支持正则表达式)，把 Li 替换成  Wang</span></span><br><span class="line"><span class="comment">#names_replace=($&#123;names_part[@]//Li/Wang&#125;); # 同上</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_replace contains <span class="variable">$&#123;names_replace[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:0&#125;</span>);</span><br><span class="line">names_replace=(<span class="variable">$&#123;names_part[@]/#S/X&#125;</span>); <span class="comment"># 数组替换，#S 表示S是单词的第一个字母</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_replace contains <span class="variable">$&#123;names_replace[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:0&#125;</span>);</span><br><span class="line">names_replace=(<span class="variable">$&#123;names_part[@]/%i/X&#125;</span>); <span class="comment"># 数组替换，%i 表示 i 是单词的最后一个字母</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_replace contains <span class="variable">$&#123;names_replace[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:0&#125;</span>);</span><br><span class="line">names_delete=(<span class="variable">$&#123;names_part[@]/Z*ang&#125;</span>); <span class="comment"># 数组删除</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_delete contains <span class="variable">$&#123;names_delete[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># ksh 不支持 行首，行尾，单词边界 正则表达式，下面的方式实现了类似功能</span></span><br><span class="line">names_part=(<span class="variable">$&#123;names[@]:0&#125;</span>);</span><br><span class="line">names_delete=(<span class="variable">$&#123;names_part[@]/#Z*ang&#125;</span>); <span class="comment"># 数组删除，# 匹配开头</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_delete contains <span class="variable">$&#123;names_delete[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">names_delete=(<span class="variable">$&#123;names_part[@]/%*n&#125;</span>); <span class="comment"># 数组删除，% 匹配结尾</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_delete contains <span class="variable">$&#123;names_delete[@]&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 迭代数组</span></span><br><span class="line"><span class="comment"># for 循环迭代,注意此处有双引号哦，</span></span><br><span class="line"><span class="keyword">for</span> e <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$&#123;names[@]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="variable">$e</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># while 循环迭代</span></span><br><span class="line">i=0;</span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -lt <span class="variable">$&#123;#names[@]&#125;</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th of names is <span class="variable">$&#123;names[$i]&#125;</span>&quot;</span>;</span><br><span class="line">    ((i++));</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 删除数组</span></span><br><span class="line"><span class="built_in">unset</span> names[1]; <span class="comment"># 删除下标 1的元素</span></span><br><span class="line"><span class="built_in">unset</span> names; <span class="comment"># 删除数组</span></span><br></pre></td></tr></table></figure>

<p>我们可以通过 ${names[@]} 或 ${names[*]} 得到所有数组元素，但是它们有很大的区别，下面的例子演示了它们的区别。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 定义数组</span></span><br><span class="line">names=(<span class="string">&quot;abc&quot;</span> <span class="string">&quot;x yz&quot;</span> <span class="string">&quot;1 2 \n3&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 访问数组</span></span><br><span class="line">length=<span class="variable">$&#123;#names[*]&#125;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The length of array from #names[*] is <span class="variable">$length</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">length=<span class="string">&quot;<span class="variable">$&#123;#names[*]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The length of array from \&quot;#names[*]\&quot; is <span class="variable">$length</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">length=<span class="variable">$&#123;#names[@]&#125;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The length of array from #names[@] is <span class="variable">$length</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line">length=<span class="string">&quot;<span class="variable">$&#123;#names[@]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The length of array from \&quot;#names[@]\&quot; is <span class="variable">$length</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $&#123;names[*]&#125;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> e <span class="keyword">in</span> <span class="variable">$&#123;names[*]&#125;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#--<span class="variable">$e</span>--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$&#123;names[*]&#125;&quot;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> e <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$&#123;names[*]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#--<span class="variable">$e</span>--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $&#123;names[@]&#125;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> e <span class="keyword">in</span> <span class="variable">$&#123;names[@]&#125;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#--<span class="variable">$e</span>--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$&#123;names[@]&#125;&quot;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> e <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$&#123;names[@]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;#--<span class="variable">$e</span>--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<p>结果如下：</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">The length of array from <span class="comment">#names[*] is 3</span></span><br><span class="line">The length of array from <span class="string">&quot;#names[*]&quot;</span> is 3</span><br><span class="line">The length of array from <span class="comment">#names[@] is 3</span></span><br><span class="line">The length of array from <span class="string">&quot;#names[@]&quot;</span> is 3</span><br><span class="line">iterating array <span class="variable">$&#123;names[*]&#125;</span></span><br><span class="line"><span class="comment">#--abc--#</span></span><br><span class="line"><span class="comment">#--x--#</span></span><br><span class="line"><span class="comment">#--yz--#</span></span><br><span class="line"><span class="comment">#--1--#</span></span><br><span class="line"><span class="comment">#--2--#</span></span><br><span class="line"><span class="comment">#--</span></span><br><span class="line">3--<span class="comment">#</span></span><br><span class="line">iterating array <span class="string">&quot;<span class="variable">$&#123;names[*]&#125;</span>&quot;</span></span><br><span class="line"><span class="comment">#--abc x yz 1 2 </span></span><br><span class="line">3--<span class="comment">#</span></span><br><span class="line">iterating array <span class="variable">$&#123;names[@]&#125;</span></span><br><span class="line"><span class="comment">#--abc--#</span></span><br><span class="line"><span class="comment">#--x--#</span></span><br><span class="line"><span class="comment">#--yz--#</span></span><br><span class="line"><span class="comment">#--1--#</span></span><br><span class="line"><span class="comment">#--2--#</span></span><br><span class="line"><span class="comment">#--</span></span><br><span class="line">3--<span class="comment">#</span></span><br><span class="line">iterating array <span class="string">&quot;<span class="variable">$&#123;names[@]&#125;</span>&quot;</span></span><br><span class="line"><span class="comment">#--abc--#</span></span><br><span class="line"><span class="comment">#--x yz--#</span></span><br><span class="line"><span class="comment">#--1 2 </span></span><br><span class="line">3--<span class="comment">#</span></span><br></pre></td></tr></table></figure>

<p>迭代数组时，只有 “${names[@]}” 能保证正确，注意有双引号的哦。</p>
<h2 id="关联数组（Associative-Arrays）"><a href="#关联数组（Associative-Arrays）" class="headerlink" title="关联数组（Associative Arrays）"></a>关联数组（Associative Arrays）</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 定义数组</span></span><br><span class="line"><span class="comment"># 申明关联数组</span></span><br><span class="line"><span class="built_in">typeset</span> -A country_code_name_map;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 赋值方法 1</span></span><br><span class="line">country_code_name_map[CN]=<span class="string">&#x27;CHINA&#x27;</span>;</span><br><span class="line">country_code_name_map[JP]=<span class="string">&#x27;JAPAN&#x27;</span>;</span><br><span class="line">country_code_name_map[US]=<span class="string">&#x27;UNITED STATES&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 赋值方法 2</span></span><br><span class="line">country_code_name_map=([CN]=<span class="string">&#x27;CHINA&#x27;</span> [JP]=<span class="string">&#x27;JAPAN&#x27;</span> [US]=<span class="string">&#x27;UNITED STATES&#x27;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 访问数组</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;CN = <span class="variable">$&#123;country_code_name_map[CN]&#125;</span>&quot;</span>; <span class="comment"># 取出数组键为 CN 的值</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;country name are <span class="variable">$&#123;country_code_name_map[*]&#125;</span>&quot;</span>; <span class="comment"># 取出所有值</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;country name are <span class="variable">$&#123;country_code_name_map[@]&#125;</span>&quot;</span>; <span class="comment"># 取出所有值</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Having <span class="variable">$&#123;#country_code_name_map[*]&#125;</span> country names&quot;</span>; <span class="comment"># 数组长度</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Having  <span class="variable">$&#123;#country_code_name_map[@]&#125;</span> country names&quot;</span>; <span class="comment"># 数组长度</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;country code are <span class="variable">$&#123;!country_code_name_map[*]&#125;</span>&quot;</span>; <span class="comment"># 取出所有键</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;country code are <span class="variable">$&#123;!country_code_name_map[@]&#125;</span>&quot;</span>; <span class="comment"># 取出所有键</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 操作数组</span></span><br><span class="line">country_code_name_map+=([KR]=<span class="string">&#x27;SOUTH KOREA&#x27;</span>); <span class="comment"># 向数组中添加数据</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组提取，替换，删除操作和上面一致</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 迭代数组</span></span><br><span class="line"><span class="comment"># for 循环迭代</span></span><br><span class="line"><span class="keyword">for</span> index <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$&#123;!country_code_name_map[@]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$index</span> = <span class="variable">$&#123;country_code_name_map[$index]&#125;</span>&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">################################ 删除数组</span></span><br><span class="line"><span class="built_in">unset</span> country_code_name_map[KR]; <span class="comment"># 删除键为 KR的元素</span></span><br><span class="line"><span class="built_in">unset</span> country_code_name_map; <span class="comment"># 删除数组</span></span><br></pre></td></tr></table></figure>

<h1 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h1><p>引用相当于一个别名。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> name=<span class="string">&quot;Shangbo&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义引用变量方法 1</span></span><br><span class="line"><span class="built_in">typeset</span> -n name_ref1=name;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;name_ref1=<span class="variable">$name_ref1</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义引用变量方法 2</span></span><br><span class="line">nameref name_ref2=name;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;name_ref2=<span class="variable">$name_ref2</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 得到实际变量名</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;name_ref1 reference <span class="variable">$&#123;!name_ref1&#125;</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除引用变量</span></span><br><span class="line"><span class="built_in">unset</span> -n name_ref1;</span><br><span class="line"><span class="built_in">unset</span> -n name_ref2;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组的引用</span></span><br><span class="line">names=(<span class="string">&quot;Shang Bo&quot;</span> <span class="string">&quot;Zhang San&quot;</span> <span class="string">&quot;Li Si&quot;</span>);</span><br><span class="line"><span class="built_in">typeset</span> -n names_ref=names;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;names_ref=<span class="variable">$&#123;names_ref[@]&#125;</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h1 id="控制结构"><a href="#控制结构" class="headerlink" title="控制结构"></a>控制结构</h1><h2 id="if-语句"><a href="#if-语句" class="headerlink" title="if 语句"></a>if 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i x=20;</span><br><span class="line"><span class="built_in">typeset</span> -i y=3;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -gt <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt;= <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -gt <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt;= <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$x</span> -gt <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &gt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">elif</span> [[ <span class="variable">$x</span> -eq <span class="variable">$y</span> ]]; <span class="keyword">then</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> = <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#** <span class="variable">$x</span> &lt; <span class="variable">$y</span> **#&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<h2 id="case-语句"><a href="#case-语句" class="headerlink" title="case 语句"></a>case 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> opt=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> <span class="variable">$opt</span> <span class="keyword">in</span></span><br><span class="line">	a) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span> is a&quot;</span>;;</span><br><span class="line">	b) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span> is b&quot;</span>;;</span><br><span class="line">	c|d) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span> is c or d&quot;</span>;;</span><br><span class="line">	*) <span class="built_in">print</span> <span class="string">&quot;Option not support...&quot;</span>;;</span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure>

<h2 id="while-语句"><a href="#while-语句" class="headerlink" title="while 语句"></a>while 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i n=0;</span><br><span class="line"><span class="built_in">typeset</span> -i sum=0;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> [[ <span class="variable">$n</span> -lt 11 ]]; <span class="keyword">do</span></span><br><span class="line">	(( sum += n++ ));</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;sum=<span class="variable">$sum</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h2 id="until-语句"><a href="#until-语句" class="headerlink" title="until 语句"></a>until 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i n=0;</span><br><span class="line"><span class="built_in">typeset</span> -i sum=0;</span><br><span class="line"></span><br><span class="line">until [[ <span class="variable">$n</span> -gt 10 ]]; <span class="keyword">do</span></span><br><span class="line">	(( sum += n++ ));</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;sum=<span class="variable">$sum</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<h2 id="for-语句"><a href="#for-语句" class="headerlink" title="for 语句"></a>for 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 格式1</span></span><br><span class="line"><span class="built_in">integer</span> sum=0;</span><br><span class="line"><span class="keyword">for</span> ((i = 1; i &lt;= 10; i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    ((sum+=i));</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="built_in">print</span> <span class="variable">$sum</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 格式2</span></span><br><span class="line"><span class="keyword">for</span> foo <span class="keyword">in</span> $(ls); <span class="keyword">do</span></span><br><span class="line">   <span class="keyword">if</span> [[ -d <span class="variable">$foo</span> ]];<span class="keyword">then</span></span><br><span class="line">      <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$foo</span> is a directory&quot;</span></span><br><span class="line">   <span class="keyword">else</span></span><br><span class="line">      <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$foo</span> is a file&quot;</span></span><br><span class="line">   <span class="keyword">fi</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<h2 id="select-语句"><a href="#select-语句" class="headerlink" title="select 语句"></a>select 语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># select 语句应用在交互式程序，显示一个菜单供用户输入</span></span><br><span class="line">select opt <span class="keyword">in</span> a b c d; <span class="keyword">do</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment"># $REPLY 是个内置变量，存储用户输入的数字</span></span><br><span class="line">	<span class="keyword">case</span> <span class="variable">$REPLY</span> <span class="keyword">in</span></span><br><span class="line">		1 ) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span>&quot;</span>; <span class="built_in">break</span>;;</span><br><span class="line">		2 ) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span>&quot;</span>; <span class="built_in">break</span>;;</span><br><span class="line">		3 ) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span>&quot;</span>; <span class="built_in">break</span>;;</span><br><span class="line">		4 ) <span class="built_in">print</span> <span class="string">&quot;<span class="variable">$opt</span>&quot;</span>; <span class="built_in">break</span>;;</span><br><span class="line">		* ) <span class="built_in">print</span> <span class="string">&quot;invalid, please select again.&quot;</span>;;</span><br><span class="line">	<span class="keyword">esac</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<h2 id="循环控制语句"><a href="#循环控制语句" class="headerlink" title="循环控制语句"></a>循环控制语句</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># break：退出循环  </span></span><br><span class="line"><span class="comment"># continue：执行下一循环  </span></span><br></pre></td></tr></table></figure>

<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment">################### 函数必须先定义后使用</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义函数方式 1: ksh 语法</span></span><br><span class="line"><span class="keyword">function</span> fun_test1 &#123;</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;This is function fun_test1.&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义函数方式 2: POSIX 语法</span></span><br><span class="line"><span class="function"><span class="title">fun_test2</span></span> ()&#123;</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;This is function fun_test2.&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 参数</span></span><br><span class="line"><span class="keyword">function</span> fun_test3 &#123;</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;Enter function <span class="variable">$0</span>&quot;</span>; <span class="comment"># $0 表示函数名</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;The first parameter is <span class="variable">$1</span>&quot;</span>; <span class="comment"># $1 引用第一个参数</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;The second parameter is <span class="variable">$2</span>&quot;</span>; <span class="comment"># $2 引用第二个参数</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;This function has <span class="variable">$#</span> parameters&quot;</span>; <span class="comment"># $# 表示参数的个数</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;You are calling <span class="variable">$0</span> $*&quot;</span>; <span class="comment"># $* 是个数组，存储所有的参数</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;You are calling <span class="variable">$0</span> <span class="variable">$@</span>&quot;</span>; <span class="comment"># $@ 是个数组，存储所有的参数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># return 语句返回代码给调用者</span></span><br><span class="line"><span class="comment"># 0 代表函数执行成功， 其他值代表失败</span></span><br><span class="line"><span class="keyword">function</span> equal &#123;</span><br><span class="line">	<span class="keyword">if</span> [[ -z <span class="variable">$1</span> || -z <span class="variable">$2</span> ]];<span class="keyword">then</span>  </span><br><span class="line">    	<span class="built_in">return</span> 1; </span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$1</span> = <span class="variable">$2</span> ]; <span class="keyword">then</span>  </span><br><span class="line">	    <span class="built_in">return</span> 0;</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">return</span> 2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据全局变量  max_value 得到函数的返回值</span></span><br><span class="line"><span class="keyword">function</span> max &#123;</span><br><span class="line">	<span class="keyword">if</span> [[ <span class="variable">$1</span> -gt <span class="variable">$2</span> ]]; <span class="keyword">then</span>  </span><br><span class="line">    	max_value=<span class="variable">$1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		max_value=<span class="variable">$2</span>;</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据引用得到函数的返回值</span></span><br><span class="line"><span class="keyword">function</span> max2 &#123;</span><br><span class="line">	<span class="built_in">typeset</span> -n r=<span class="variable">$3</span>; <span class="comment"># 设置变量 r 引用 $3</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> [[ <span class="variable">$1</span> -gt <span class="variable">$2</span> ]]; <span class="keyword">then</span>  </span><br><span class="line">    	r=<span class="variable">$1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		r=<span class="variable">$2</span>;</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据标准输出得到函数的返回值</span></span><br><span class="line"><span class="keyword">function</span> min &#123;</span><br><span class="line">	<span class="keyword">if</span> [[ <span class="variable">$1</span> -lt <span class="variable">$2</span> ]]; <span class="keyword">then</span>  </span><br><span class="line">    	<span class="built_in">print</span> <span class="variable">$1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">print</span> <span class="variable">$2</span>;</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="调用"><a href="#调用" class="headerlink" title="调用"></a>调用</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment">################### Main</span></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">fun_test1;</span><br><span class="line">fun_test2;</span><br><span class="line">fun_test3 a b c d;</span><br><span class="line"></span><br><span class="line">equal;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Call equal function end with error code $?&quot;</span>; <span class="comment"># $? 表示上个命令返回的代码</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i max_value=0;</span><br><span class="line">max 1 2;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;the bigger value of 1 and 2 is <span class="variable">$max_value</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">typeset</span> -i max_value=0;</span><br><span class="line">max2 3 4 max_value;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;the bigger value of 3 and 4 is <span class="variable">$max_value</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># `` 用来捕获函数或命令的标准输入到变量中</span></span><br><span class="line">smaller=`min 1 2`;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;the smaller value of 1 and 2 is <span class="variable">$smaller</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># $() 用来捕获函数或命令的标准输入到变量中</span></span><br><span class="line">smaller=$(min 3 4）;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;the smaller value of 3 and 4 is <span class="variable">$smaller</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除函数</span></span><br><span class="line"><span class="built_in">unset</span> -f fun_test1;</span><br><span class="line"><span class="built_in">unset</span> -f fun_test2;</span><br></pre></td></tr></table></figure>



<h2 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h2><p>我们可以从 $* 或 $@ 数组中得到函数的所有参数，但是它们有非常大的区别。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">################### 函数</span></span><br><span class="line"><span class="keyword">function</span> fun_test &#123;</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&#x27;iterating array $*&#x27;</span>;  </span><br><span class="line">	<span class="built_in">typeset</span> -i i=1;</span><br><span class="line">	<span class="keyword">for</span> p <span class="keyword">in</span> $*;  </span><br><span class="line">	<span class="keyword">do</span>  </span><br><span class="line">	    <span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;  </span><br><span class="line">	    ((i++));  </span><br><span class="line">	<span class="keyword">done</span>;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$*&quot;&#x27;</span>;</span><br><span class="line">	i=1;</span><br><span class="line">	<span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">&quot;$*&quot;</span>;  </span><br><span class="line">	<span class="keyword">do</span>  </span><br><span class="line">	    <span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;  </span><br><span class="line">	    ((i++));  </span><br><span class="line">	<span class="keyword">done</span>;	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&#x27;iterating array $@&#x27;</span>;</span><br><span class="line">	i=1;  </span><br><span class="line">	<span class="keyword">for</span> p <span class="keyword">in</span> <span class="variable">$@</span>;  </span><br><span class="line">	<span class="keyword">do</span>  </span><br><span class="line">	    <span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;  </span><br><span class="line">	    ((i++));  </span><br><span class="line">	<span class="keyword">done</span>;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$@&quot;&#x27;</span>;</span><br><span class="line">	i=1;</span><br><span class="line">	<span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$@</span>&quot;</span>;  </span><br><span class="line">	<span class="keyword">do</span>  </span><br><span class="line">	    <span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;  </span><br><span class="line">	    ((i++));  </span><br><span class="line">	<span class="keyword">done</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">################### Main</span></span><br><span class="line">fun_test abc <span class="string">&quot;x yz&quot;</span> <span class="string">&quot;1 2 \n3&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>结果：</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">iterating array $*</span><br><span class="line">The 1th parameter is [abc]</span><br><span class="line">The 2th parameter is [x]</span><br><span class="line">The 3th parameter is [yz]</span><br><span class="line">The 4th parameter is [1]</span><br><span class="line">The 5th parameter is [2]</span><br><span class="line">The 6th parameter is [</span><br><span class="line">3]</span><br><span class="line"></span><br><span class="line">iterating array <span class="string">&quot;$*&quot;</span></span><br><span class="line">The 1th parameter is [abc x yz 1 2 </span><br><span class="line">3]</span><br><span class="line"></span><br><span class="line">iterating array <span class="variable">$@</span></span><br><span class="line">The 1th parameter is [abc]</span><br><span class="line">The 2th parameter is [x]</span><br><span class="line">The 3th parameter is [yz]</span><br><span class="line">The 4th parameter is [1]</span><br><span class="line">The 5th parameter is [2]</span><br><span class="line">The 6th parameter is [</span><br><span class="line">3]</span><br><span class="line"></span><br><span class="line">iterating array <span class="string">&quot;<span class="variable">$@</span>&quot;</span></span><br><span class="line">The 1th parameter is [abc]</span><br><span class="line">The 2th parameter is [x yz]</span><br><span class="line">The 3th parameter is [1 2 </span><br><span class="line">3]</span><br></pre></td></tr></table></figure>

<p>从上面的结果中可以看出，<strong>只有 “$@” 能保证正确</strong>，注意，有双引号的哦。</p>
<h2 id="内置函数"><a href="#内置函数" class="headerlink" title="内置函数"></a>内置函数</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">函数        返回值</span><br><span class="line">abs	        Absolute value	</span><br><span class="line">hypot       Euclidean distance</span><br><span class="line">acos	    Arc cosine</span><br><span class="line">int         Integer part</span><br><span class="line">sin	        Arc sine</span><br><span class="line"><span class="built_in">log</span>	        Natural logarithm</span><br><span class="line">atan        Arc tangent</span><br><span class="line">pow         Exponentiation (xy)</span><br><span class="line">atan2       Arc tangent of two variables</span><br><span class="line">sin	        Sine</span><br><span class="line">cos	        Cosine</span><br><span class="line">sinh        Hyperbolic sine</span><br><span class="line">cosh	    Hyperbolic cosine</span><br><span class="line">sqrt        Square root</span><br><span class="line">exp         Exponential (ex)</span><br><span class="line">tan	        Tangent</span><br><span class="line">fmod        Floating-point remainder</span><br><span class="line">tanh        Hyperbolic tangent</span><br></pre></td></tr></table></figure>

<h1 id="命令退出状态"><a href="#命令退出状态" class="headerlink" title="命令退出状态"></a>命令退出状态</h1><p>每个 Linux 命令，脚本或函数都有一个退出状态，它用来指示该命令，脚本或函数是否执行成功。0 代表成功，其他值（1-255） 代表失败。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">退出状态   描述</span><br><span class="line">1-125    命令执行失败</span><br><span class="line">2        命令使用错误，如参数传递不正确</span><br><span class="line">126      命令不可执行</span><br><span class="line">127      找不到命令</span><br><span class="line">128-255  外部命令执行失败</span><br><span class="line">256	     由于一个信号，命令终止</span><br></pre></td></tr></table></figure>

<p>对于函数来说，我们可以使用 return 语句来返回退出状态，如果一个函数没有 return 语句，默认返回函数中最后执行语句的的退出状态。对于脚本来说，我们可以使用 exit 语句来返回退出状态，如果一个脚本没有 exit 语句，默认返回脚本中最后执行语句的的退出状态。有一点需要特别注意，当脚本中的一条命令执行失败时，整个脚本并不会停止运行，所以对于可能出错的命令，我们最好检查它的退出状态，我们可以通过变量 $? 来得到它的退出状态。下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line">grep <span class="built_in">test</span> test.txt &gt; /dev/null;</span><br><span class="line"></span><br><span class="line">exit_code=$?;</span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$exit_code</span> -ne 0 ]]; <span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;command failed.&quot;</span></span><br><span class="line">	<span class="built_in">exit</span> 1;</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>事实上，我们可以把命令直接写在 if 语句中，如下：</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> grep <span class="built_in">test</span> test.txt &gt; /dev/null; <span class="keyword">then</span>  </span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;command success.&quot;</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;command failed.&quot;</span>;</span><br><span class="line">    <span class="built_in">exit</span> 1;</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>如果每条语句都这么检查退出状态，那么你的脚本会很长，其实，我们还可以这么写。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 利用逻辑运算符的短路特性</span></span><br><span class="line">grep <span class="built_in">test</span> test.txt &gt; /dev/null &amp;&amp; <span class="built_in">print</span> <span class="string">&quot;command success.&quot;</span></span><br><span class="line">grep <span class="built_in">test</span> test.txt &gt; /dev/null || <span class="built_in">print</span> <span class="string">&quot;command failed.&quot;</span>;<span class="built_in">exit</span> 1</span><br></pre></td></tr></table></figure>

<h1 id="向脚本传递参数"><a href="#向脚本传递参数" class="headerlink" title="向脚本传递参数"></a>向脚本传递参数</h1><h2 id="方法1"><a href="#方法1" class="headerlink" title="方法1"></a>方法1</h2><p>如果你的脚本只需要很少的参数，你可以通过 $1,$2 … $n 直接引用它们。下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># $0 表示脚本名</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The script name is <span class="variable">$0</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The first parameter is <span class="variable">$1</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The Second parameter is <span class="variable">$2</span>&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>你也可以使用 shift 删除前面的参数即可，后面的参数会自动前移，下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The first parameter is <span class="variable">$1</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># shift 删除第一个参数，同时将后面的参数前移</span></span><br><span class="line"><span class="built_in">shift</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The Second parameter is <span class="variable">$1</span>&quot;</span>;<span class="comment"># 注意此次还是 $1</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The third parameter is <span class="variable">$2</span>&quot;</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The fourth parameter is <span class="variable">$3</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># shift 删除前三个参数，同时将后面的参数前移</span></span><br><span class="line"><span class="built_in">shift</span> 3;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;The fifth parameter is <span class="variable">$1</span>&quot;</span>;<span class="comment"># 注意此次还是 $1</span></span><br></pre></td></tr></table></figure>



<h2 id="方法2"><a href="#方法2" class="headerlink" title="方法2"></a>方法2</h2><p>事实上，ksh 会自动将所有的参数放到数组 $@ 和 $* 数组中，你可以迭代它来得到所有的参数。下面是一个简单的例子</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- $# 表示参数的数量 ---------------#</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;This script has <span class="variable">$#</span> parameters.&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 $@ ---------------#</span></span><br><span class="line"><span class="built_in">typeset</span> -i i=1;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $@&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> <span class="variable">$@</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;</span><br><span class="line">	((i++));</span><br><span class="line"><span class="keyword">done</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 $* ---------------#</span></span><br><span class="line">i=0;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $*&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> $*;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;The <span class="variable">$&#123;i&#125;</span>th parameter is [<span class="variable">$p</span>]&quot;</span>;</span><br><span class="line">	((i++));</span><br><span class="line"><span class="keyword">done</span>;</span><br></pre></td></tr></table></figure>

<p>ksh 为什么会将参数放在两个数组中（$@，$*），它们有什么区别呢？下面的例子演示了它们之间的区别。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 $*---------------#</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $*&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> $*;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#--[<span class="variable">$p</span>]--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 &quot;$*&quot;,注意数组用双引号括起来 ---------------#</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$*&quot;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">&quot;$*&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#--[<span class="variable">$p</span>]--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 $@ ---------------#</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array $@&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> <span class="variable">$@</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#--[<span class="variable">$p</span>]--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#-------------- 迭代数组 &quot;$@&quot;,注意数组用双引号括起来 ---------------#</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&#x27;iterating array &quot;$@&quot;&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$@</span>&quot;</span>;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;#--[<span class="variable">$p</span>]--#&quot;</span>;</span><br><span class="line"><span class="keyword">done</span>;</span><br></pre></td></tr></table></figure>

<p>结果如下：</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">iterating array $*</span><br><span class="line"><span class="comment">#--[abc]--#</span></span><br><span class="line"><span class="comment">#--[x]--#</span></span><br><span class="line"><span class="comment">#--[yz]--#</span></span><br><span class="line"><span class="comment">#--[1]--#</span></span><br><span class="line"><span class="comment">#--[2</span></span><br><span class="line">3]--<span class="comment">#</span></span><br><span class="line">iterating array <span class="string">&quot;$*&quot;</span></span><br><span class="line"><span class="comment">#--[abc x yz 1 2</span></span><br><span class="line">3]--<span class="comment">#</span></span><br><span class="line">iterating array <span class="variable">$@</span></span><br><span class="line"><span class="comment">#--[abc]--#</span></span><br><span class="line"><span class="comment">#--[x]--#</span></span><br><span class="line"><span class="comment">#--[yz]--#</span></span><br><span class="line"><span class="comment">#--[1]--#</span></span><br><span class="line"><span class="comment">#--[2</span></span><br><span class="line">3]--<span class="comment">#</span></span><br><span class="line">iterating array <span class="string">&quot;<span class="variable">$@</span>&quot;</span></span><br><span class="line"><span class="comment">#--[abc]--#</span></span><br><span class="line"><span class="comment">#--[x yz]--#</span></span><br><span class="line"><span class="comment">#--[1 2</span></span><br><span class="line">3]--<span class="comment">#</span></span><br></pre></td></tr></table></figure>

<p>从上面的结果可以看到，只有”$@”能保证正确，注意有双引号哦。</p>
<h2 id="方法3"><a href="#方法3" class="headerlink" title="方法3"></a>方法3</h2><p>如果你想像 Linux 命令那样传递参数，你需要用到函数 getopts。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># OPTIND 表示当前参数的索引</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;Current parameter index is <span class="variable">$OPTIND</span>&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用此脚本的方式： ./test.ksh -a -b &#x27;test&#x27; -c 10 eee ff g</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># getopts 函数有两个参数</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># 第一个参数（:ab:c#）是该脚本支持的选项</span></span><br><span class="line"><span class="comment"># 最前面的冒号表示  getopts 抑制错误消息，由脚本自己提供错误处理</span></span><br><span class="line"><span class="comment"># b 后的冒号表示改选项需要一个字符参数 （-b &#x27;test&#x27;）</span></span><br><span class="line"><span class="comment"># c 后的井号表示改选项需要一个数字参数 （-c 10）</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># 第二个参数  optname 是一个变量名，接收函数返回的选项</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">getopts</span> <span class="string">&quot;:ab:c#&quot;</span> optname</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">case</span> <span class="string">&quot;<span class="variable">$optname</span>&quot;</span> <span class="keyword">in</span></span><br><span class="line">		<span class="string">&quot;a&quot;</span>)</span><br><span class="line">			<span class="built_in">print</span> <span class="string">&quot;Option <span class="variable">$optname</span> is specified&quot;</span></span><br><span class="line">			;;</span><br><span class="line">		<span class="string">&quot;b&quot;</span>)</span><br><span class="line">			<span class="comment"># $OPTARG 保存选项后面的参数值，此例是 test</span></span><br><span class="line">			<span class="built_in">print</span> <span class="string">&quot;Option <span class="variable">$optname</span> has value <span class="variable">$OPTARG</span>&quot;</span></span><br><span class="line">			;;</span><br><span class="line">		<span class="string">&quot;c&quot;</span>)</span><br><span class="line">			<span class="built_in">print</span> <span class="string">&quot;Option <span class="variable">$optname</span> has value <span class="variable">$OPTARG</span>&quot;</span></span><br><span class="line">			;;</span><br><span class="line">		<span class="string">&quot;?&quot;</span>) <span class="comment"># ? 表示发现不能识别的选项</span></span><br><span class="line">			<span class="built_in">print</span> <span class="string">&quot;Unknown option <span class="variable">$OPTARG</span>&quot;</span></span><br><span class="line">			<span class="built_in">exit</span> -1</span><br><span class="line">			;;</span><br><span class="line">		<span class="string">&quot;:&quot;</span>) <span class="comment"># : 表示发现某个选项需要值，但是找不到这个值， 此时  OPTARG 包含丢失参数的选项</span></span><br><span class="line">			<span class="built_in">print</span> <span class="string">&quot;No argument value for option <span class="variable">$OPTARG</span>&quot;</span></span><br><span class="line">			<span class="built_in">exit</span> -1</span><br><span class="line">			;;</span><br><span class="line">		*)</span><br><span class="line">       <span class="built_in">print</span> <span class="string">&quot;Unknown error while processing options&quot;</span></span><br><span class="line">       <span class="built_in">exit</span> -1</span><br><span class="line">       ;;</span><br><span class="line">    <span class="keyword">esac</span></span><br><span class="line">    <span class="built_in">print</span> <span class="string">&quot;Current parameter index is <span class="variable">$OPTIND</span>&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选项后面的参数 (eee ff g)</span></span><br><span class="line">argstart=<span class="variable">$OPTIND</span>;</span><br><span class="line"><span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">&quot;<span class="variable">$&#123;@:$argstart&#125;</span>&quot;</span></span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;[<span class="variable">$p</span>]&quot;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<h1 id="I-O"><a href="#I-O" class="headerlink" title="I/O"></a>I/O</h1><p>ksh 支持如下 I/O。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">重定向          描述</span><br><span class="line">&gt; file        标准输出到文件，文件存在则替换</span><br><span class="line">&gt;&gt; file       标准输出到文件，文件存在则添加到末尾</span><br><span class="line">&lt; file        从文件到标准输入</span><br><span class="line">&lt;&gt; file       以读写模式打开文件</span><br><span class="line">cmd1 | cmd2   管道; 把 cmd1 的标准输出作为 cmd2 的标准输入</span><br><span class="line">&gt;| file       Force standard output to file even <span class="keyword">if</span> noclobber is <span class="built_in">set</span></span><br><span class="line">&lt;&lt; <span class="string">label      Here-document</span></span><br><span class="line"><span class="string">&lt;&lt;- label</span>     Here-document variant</span><br><span class="line">&lt;&lt;&lt; <span class="string">label     Here-string</span></span><br><span class="line"><span class="string">n&gt; file       把文件描述符 n 输出到文件（1是标准输出，2是标准错误输出）</span></span><br><span class="line"><span class="string">n&lt; file       输入文件描述符 n （0是标准输入）</span></span><br><span class="line"><span class="string">&lt;&amp;n           从文件描述符 n重复标准输入</span></span><br><span class="line"><span class="string">&gt;&amp;n           重复标准输出到文件描述符 n</span></span><br><span class="line"><span class="string">&lt;&amp;n-          把文件描述符 n作为标准输入</span></span><br><span class="line"><span class="string">&gt;&amp;n-          把文件描述符 n作为标准输出</span></span><br><span class="line"><span class="string">&lt;&amp;-           关闭标准输入</span></span><br><span class="line"><span class="string">&gt;&amp;-           关闭标准输出</span></span><br><span class="line"><span class="string">|&amp;            Background process with I/O from parent shell</span></span><br><span class="line"><span class="string">n&lt;&amp;p          Move input from coprocess to file descriptor n</span></span><br><span class="line"><span class="string">n&gt;&amp;p          Move output to coprocess to file descriptor n</span></span><br></pre></td></tr></table></figure>

<p>大家对 &gt; file， &gt;&gt; file， &lt; file 和 cmd1 | cmd2 应该都很熟悉， 只要懂一点 Linux 的人不可能没用过。 n&gt; file 和 &gt;&amp;n 看上去挺陌生，其实你肯定也用过，看看下面的命令吧，是不是很熟悉。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">cmd &gt; t.log 2&gt;&amp;1</span><br></pre></td></tr></table></figure>

<p>什么是 Here-document （&lt;&lt; label，&lt;&lt;- label） 呢？你肯定也用过，只是不知道它的名字而已。看看下面的代码吧。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line">user=<span class="string">&quot;wave0409@163.com&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#  &lt;&lt; label</span></span><br><span class="line">mail <span class="variable">$user</span> &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">Hi $user,</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">This is a test mail, please ignore.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Regards</span></span><br><span class="line"><span class="string">EOF</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#  &lt;&lt;- label 会忽略前面的 TAB，使代码可读性更好</span></span><br><span class="line">mail <span class="variable">$user</span> &lt;&lt; <span class="string">EOF</span></span><br><span class="line"><span class="string">		Hi $user,</span></span><br><span class="line"><span class="string">		</span></span><br><span class="line"><span class="string">		This is a test mail, please ignore.</span></span><br><span class="line"><span class="string">		</span></span><br><span class="line"><span class="string">		Regards</span></span><br><span class="line"><span class="string">		EOF</span></span><br></pre></td></tr></table></figure>

<p>什么是 Here-string 呢？看看下面的代码吧。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line">user=<span class="string">&quot;wave0409@163.com&quot;</span>;</span><br><span class="line">text=<span class="string">&quot;this is a test mail.&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用管道</span></span><br><span class="line"><span class="built_in">print</span> <span class="variable">$text</span> | mail <span class="variable">$user</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#  &lt;&lt;&lt; label</span></span><br><span class="line">mail <span class="variable">$user</span> &lt;&lt;&lt; <span class="variable">$text</span>;</span><br></pre></td></tr></table></figure>

<p>通常，Linux 命令只接受一个输入和一个输出，但是通过下面的方式，我们可以接受多个输入和多个输出。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">cmd  &lt;(input) &lt;(input)</span><br><span class="line">cmd  &gt;(output) &gt;(output)</span><br></pre></td></tr></table></figure>

<p>下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将原始数据test.txt，1)排序后输出test.txt.sort 2) 取第二列输出test.txt.id 3) 保存原始数据 test.txt.raw</span></span><br><span class="line">cat test.txt | tee &gt;(sort | uniq &gt; test.txt.sort) &gt;(cut -d, -f2 test.txt &gt; test.txt.id) &gt; test.txt.raw</span><br></pre></td></tr></table></figure>

<h1 id="格式化输出"><a href="#格式化输出" class="headerlink" title="格式化输出"></a>格式化输出</h1><h2 id="简单示例"><a href="#简单示例" class="headerlink" title="简单示例"></a>简单示例</h2><p>几乎所有的语言都支持 printf 函数，ksh 也不例外。下面我们来看一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;printf example:%,30.6f&quot;</span> 9876543210.0123456789;</span><br></pre></td></tr></table></figure>

<p>结果如下:</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="built_in">printf</span> example:          9,876,543,210.012346</span><br></pre></td></tr></table></figure>

<p>%,30.6f 被称为格式符(format specifier), 格式符由以下几部分组成:</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">%[flags][width][.precision]conversion  </span><br><span class="line">%  ,      30      .6         f  </span><br></pre></td></tr></table></figure>

<h2 id="注意事项-1"><a href="#注意事项-1" class="headerlink" title="注意事项"></a>注意事项</h2><ul>
<li>%是必须的, 任何格式符都由百分号开始.</li>
<li>flags 是可选的, 详见下表.</li>
<li>width 是可选的, 表示输出的宽度.</li>
<li>precision 可选的, precision 依赖于 conversion, 详见下表.</li>
<li>conversion 是必须的, 表示如何格式化参数, 详见下表.</li>
</ul>
<h2 id="转换符-conversion"><a href="#转换符-conversion" class="headerlink" title="转换符(conversion)"></a>转换符(conversion)</h2><p>ksh 支持如下转换符(conversion)。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">转换符           描述</span><br><span class="line"> d               十进制整数,也可进制转换</span><br><span class="line"> i               十进制整数</span><br><span class="line"> u               无符号十进制整数 </span><br><span class="line"> e               浮点数科学计数法</span><br><span class="line"> E               浮点数科学计数法</span><br><span class="line"> f               浮点数</span><br><span class="line"> g               %e 或 %f, 取决于哪个更短, 删除尾部0</span><br><span class="line"> G               %E 或 %f, 取决于哪个更短, 删除尾部0</span><br><span class="line"> o               无符号八进制整数</span><br><span class="line"> x               无符号十六进制整数（a-f for 10 to 15）</span><br><span class="line"> X               无符号十六进制整数（A-F for 10 to 15）</span><br><span class="line"> c               ASCII 字符 (打印第一个字符)</span><br><span class="line"> %               %</span><br><span class="line"> s               字符串</span><br><span class="line"> b                %s，支持更多的转义字符</span><br><span class="line"> H                %s，转成 HTML 字符，如：空格转成  </span><br><span class="line"> P                %s，把 egrep 风格的正则表达式转成 ksh 风格</span><br><span class="line"> R                %s，把 ksh 风格的正则表达式转成 egrep 风格</span><br><span class="line"> q                %s，打印成 $&#x27;...&#x27; 风格</span><br><span class="line"> n               到字符串的长度</span><br><span class="line"> (date format)T  格式化日期</span><br></pre></td></tr></table></figure>

<h2 id="日期和时间转换符"><a href="#日期和时间转换符" class="headerlink" title="日期和时间转换符"></a>日期和时间转换符</h2><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">转换符(Conversion)	描述(Description)</span><br><span class="line">Y	                4位数字的年(前面补0)</span><br><span class="line">m	                月,01-12</span><br><span class="line">d	                日,01-31</span><br><span class="line">H	                小时,00-23</span><br><span class="line">M	                分钟,00-59</span><br><span class="line">S	                秒,00-60</span><br><span class="line">I	                小时,01-12</span><br><span class="line">N	                纳秒,000000000 - 999999999</span><br><span class="line">P	                本地敏感, PM or AM</span><br><span class="line">p	                本地敏感,pm or am</span><br><span class="line">z	                时区偏移量, 如 +0800</span><br><span class="line">Z	                时区</span><br><span class="line">s	                从格林威治时间1970-01-01 00:00:00起的秒数</span><br><span class="line">B	                月的完整拼写</span><br><span class="line">b/h	                月的缩写</span><br><span class="line">A	                星期的完整拼写</span><br><span class="line">a	                星期的缩写</span><br><span class="line">y	                年的后两位数字(前面补0)</span><br><span class="line">C	                年的前两位数字(前面补0)</span><br><span class="line">j	                一年的第几天,001-366</span><br><span class="line">e	                日,1-31</span><br><span class="line">R	                24小时时间,如 18:05</span><br><span class="line">T	                24小时时间,如 18:05:19</span><br><span class="line">r	                12小时时间, 如 06:05:19 pm</span><br><span class="line">D	                美国格式的日期(月/日/年),如 02/09/2004</span><br><span class="line">F	                ISO 8601 日期, 如 2004-02-09</span><br><span class="line">c	                完整的日期和时间,如 Tue Oct  6 09:15:27 2015</span><br><span class="line">k	                完整的日期和时间,如 Tue Oct  6 09:18:22 EDT 2015</span><br><span class="line">l                   完整的日期和时间,如 Oct  6 09:19</span><br><span class="line">L                   完整的日期和时间,如  Tue Oct  6 09:20:17 EDT 2015</span><br></pre></td></tr></table></figure>

<h2 id="标志-flag"><a href="#标志-flag" class="headerlink" title="标志(flag)"></a>标志(flag)</h2><p>ksh 支持如下标志(flag)</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">标志         描述               举例</span><br><span class="line">-           左对齐             |3333.33 |</span><br><span class="line">空格         在正数之前添加空格    | 3333.33|，|-3333.33|</span><br><span class="line">+           打印正负数符号       |+3333.33|，|-3333.33|</span><br><span class="line">0           数字前面补0         |003333.33|</span><br><span class="line">,           添加千分位          |3,333.33|</span><br><span class="line">#(对于%o)    添加前缀0           |0515|</span><br><span class="line">#(对于%x)    添加前缀0x	      |0x1bc|</span><br><span class="line">#(对于%X)    添加前缀0X	      |0X1bc|</span><br><span class="line">#(对于%e)    添加小数点          |1.000000e+01|</span><br><span class="line">#(对于%E)    添加小数点          |1.000000E+01|</span><br><span class="line">#(对于%f)    添加小数点          | 10.000000|</span><br><span class="line">#(对于%g)    不删除尾部0         |10.4000|</span><br><span class="line">#(对于%G)    不删除尾部0         |10.4000|</span><br></pre></td></tr></table></figure>

<h2 id="精度-precision"><a href="#精度-precision" class="headerlink" title="精度(precision)"></a>精度(precision)</h2><p>ksh 精度(precision)的意义。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">转换符                 精度意义</span><br><span class="line">%d,%i,%o,%u,%x,%X    最少数字位数，如果数字位数少于精度，添加前缀0</span><br><span class="line">%e, %E               最少数字位数，如果数字位数少于精度，添加后缀0</span><br><span class="line">%f                   小数的位数</span><br><span class="line">%g, %G               最多数字位数</span><br><span class="line">%s                   字符位数</span><br></pre></td></tr></table></figure>

<h2 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h2><figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># %b 同 %s，支持更多的转义字符</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;%b\n&quot;</span> <span class="string">&#x27;hello\nworld&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># %H 同 %s，转成 HTML 字符，如：空格转成  </span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;%H\n&quot;</span> <span class="string">&quot;Here are real &lt; and &gt; characters&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># %P 同 %s，把 egrep 风格的正则表达式转成 ksh 风格</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;%P\n&quot;</span> <span class="string">&#x27;(.*\.o|.*\.obj|core)+&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># %R 同 %s，把 ksh 风格的正则表达式转成 egrep 风格</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;%R\n&quot;</span> <span class="string">&#x27;+(*.o|*.c)&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># %q 同 %s，打印成 $&#x27;...&#x27; 风格</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;print %q\n&quot;</span> <span class="string">&quot;a string with &#x27; and \&quot; in it&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># %d，进制转换</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&#x27;42 is %.3.5d in base 5\n&#x27;</span> 42</span><br><span class="line"></span><br><span class="line"><span class="comment"># %n 得到字符串的长度</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;hello, world\n%n&quot;</span> msglen;</span><br><span class="line"><span class="built_in">print</span> <span class="variable">$msglen</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># %(date format)T, 格式化日期</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">&quot;%(It is now %Y-%m-%d %H:%M:%S)T\n&quot;</span> <span class="string">&quot;<span class="subst">$(date)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<h1 id="动态命令"><a href="#动态命令" class="headerlink" title="动态命令"></a>动态命令</h1><p>有时候我们需要动态生成一些命令，然后执行。下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line">cmd=<span class="string">&quot;ls | more&quot;</span>;</span><br><span class="line"><span class="variable">$cmd</span>;</span><br></pre></td></tr></table></figure>

<p>如果你执行上面的脚本，你会收到下面的错误消息，原因是 ksh 把 | 理解成了一个文件名。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">ls: cannot access |: No such file or directory</span><br></pre></td></tr></table></figure>

<p>遇到这种情况该如何处理呢？呵呵，so easy，你只需要在命令前加上 eval 即可，如下。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line">list=<span class="string">&quot;ls | more&quot;</span>;</span><br><span class="line"><span class="built_in">eval</span> <span class="variable">$list</span>;</span><br></pre></td></tr></table></figure>

<h1 id="多进程"><a href="#多进程" class="headerlink" title="多进程"></a>多进程</h1><h2 id="作业控制"><a href="#作业控制" class="headerlink" title="作业控制"></a>作业控制</h2><p>当我们在 ksh 中执行一条命令时，在命令结束之前，我们能做的只有等待，如果命令需要运行很长时间，在这段时间内你还想干点别的，你可以通过下面的方式运行命令。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在命令的末尾加上 &amp; 表示在后台执行该命令</span></span><br><span class="line">ls &gt; /dev/null &amp;</span><br></pre></td></tr></table></figure>

<p>当你按回车后，你马上会看到如下输出，1 表示作业号(Job Number)，3106 表示进程号(Process ID)。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">[1] 3106</span><br></pre></td></tr></table></figure>

<p>命令执行完后，会有如下输出。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">[1]+  Done                    ls --color=auto &gt; /dev/null</span><br></pre></td></tr></table></figure>

<p>如果命令执行错误，你会看到如下输出，2 表示错误代码。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">[1]+  Exit 2                  ls --color=auto dd /dev/null</span><br></pre></td></tr></table></figure>

<p>你可能想，能不能把一个正在运行的命令放在后台执行呢？答案是肯定的，你需要两步。</p>
<p>第一步，按 CTRL-Z 暂停命令，你会得到如下的输出。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">[1]+  Stopped                 vi test.txt</span><br></pre></td></tr></table></figure>

<p>第二步，执行下面的命令把它放在后台执行。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="built_in">bg</span> %1</span><br></pre></td></tr></table></figure>

<p>上面的 %1 表示作业号为 1 的作业。我们还可以通过下面的方式引用进程或作业。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">引用        进程</span><br><span class="line">N           进程号 N</span><br><span class="line">-N          进程组号 N</span><br><span class="line">%N          作业号 N</span><br><span class="line">%string	    以 string 开头的命令</span><br><span class="line">%?string    包含 string 的命令</span><br><span class="line">%+或%%      最后被调用的命令</span><br><span class="line">%-          倒数第二被调用的命令</span><br></pre></td></tr></table></figure>

<p>如果你想查看当前有哪些进程在后台执行，你可以运行 jobs 命令，如果你想把某个后台命令放在前台执行，试一试 fg %N 吧。<br>通常，当你退出登录时，你运行的所有命令，不管前台还是后台都会被终止，如果你想让某个命令在你退出登录时继续执行，试一试 disown %N 吧。当然你也可以采用如下的方式运行命令，这样即使你退出登录，也能保证不会被终止。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">nohup ls &gt; /dev/null &amp;</span><br></pre></td></tr></table></figure>



<h2 id="进程管理"><a href="#进程管理" class="headerlink" title="进程管理"></a>进程管理</h2><p>很多人认为 kill 命令就是用来杀掉进程的，其实它是用来给进程发送信号(Signal)的，我们可以给进程发送哪些信号呢？试一试下面的命令吧。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">kill -l</span><br><span class="line"></span><br><span class="line">结果如下：</span><br><span class="line"> 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP</span><br><span class="line"> 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1</span><br><span class="line">11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM</span><br><span class="line">16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP</span><br><span class="line">21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ</span><br><span class="line">26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR</span><br><span class="line">31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3</span><br><span class="line">38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8</span><br><span class="line">43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13</span><br><span class="line">48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12</span><br><span class="line">53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7</span><br><span class="line">58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2</span><br><span class="line">63) SIGRTMAX-1  64) SIGRTMAX</span><br></pre></td></tr></table></figure>

<p>下面是一些简单的例子。 </p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 3106 是进程号，我们也可以通过作业号，命令名引用进程</span></span><br><span class="line"><span class="built_in">kill</span> 3106 <span class="comment"># 发送 TERM 信号终止进程</span></span><br><span class="line"><span class="built_in">kill</span> -15 3106 <span class="comment"># 同上</span></span><br><span class="line"><span class="built_in">kill</span> -s TERM 3106 <span class="comment"># 同上</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">kill</span> -s INT 3106 <span class="comment"># 发送 INT 信号中断进程，同 CTRL-C</span></span><br><span class="line"><span class="built_in">kill</span> -s QUIT 3106 <span class="comment"># 发送 QUIT 信号退出进程</span></span><br><span class="line"><span class="built_in">kill</span> -s KILL 3106 <span class="comment"># 发送 KILL 信号杀死进程</span></span><br></pre></td></tr></table></figure>

<p>上面提到的 jobs 命令只能查询到当前 shell 启动的进程，如果你想查看所有的进程，你可以使用 ps 命令。下面是一个简单的例子。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line">ps -ef | grep <span class="string">&#x27;vi test.txt&#x27;</span> | grep -v grep</span><br></pre></td></tr></table></figure>



<h2 id="多进程-1"><a href="#多进程-1" class="headerlink" title="多进程"></a>多进程</h2><p>假设某个目录下有好多 SQL 文件，现在让你把它们都运行一遍，你可以采用如下的脚本。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh  </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 扫描所有的 SQL 语句并运行</span></span><br><span class="line"><span class="keyword">for</span> sql <span class="keyword">in</span> $(ls *.sql)</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	runSQL <span class="variable">$&#123;sql&#125;</span>;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;done&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>由于某些 SQL 需要运行很长时间，导致这个脚本很慢，其实，你可以并发执行所有的 SQL 文件，如下。</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/ksh</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> clean &#123;</span><br><span class="line">	<span class="built_in">print</span> <span class="string">&quot;received interrupt signal.&quot;</span>;</span><br><span class="line">	<span class="comment"># disconnect db and clean tmp file etc</span></span><br><span class="line">	<span class="built_in">exit</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># trap 表示如果收到 INT，TERM 或  QUIT 信号就运行函数 clean</span></span><br><span class="line"><span class="built_in">trap</span> <span class="string">&#x27;clean&#x27;</span> INT TERM QUIT</span><br><span class="line"></span><br><span class="line"><span class="comment"># 扫描所有的 SQL 语句并运行</span></span><br><span class="line"><span class="keyword">for</span> sql <span class="keyword">in</span> $(ls *.sql)</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="comment"># 在后台运行 SQL 语句</span></span><br><span class="line">	runSQL <span class="variable">$&#123;sql&#125;</span> &amp;;</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># wait 命令等待所有后台命令执行完毕</span></span><br><span class="line"><span class="built_in">wait</span>;</span><br><span class="line"><span class="built_in">print</span> <span class="string">&quot;done&quot;</span>;</span><br></pre></td></tr></table></figure>

<h1 id="控制键"><a href="#控制键" class="headerlink" title="控制键"></a>控制键</h1><p>如果你想结束一个正在运行的命令怎么办？试一试 CTRL-C 吧。 CTRL-C 是一个控制键，如何查看 Shell 都支持哪些控制键呢？试一试下面的命令吧。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">stty -a</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">控制键                 stty名    描述</span><br><span class="line">CTRL-C	              intr     终止当前命令</span><br><span class="line">CTRL-\ or CTRL-|      quit	   终止当前命令</span><br><span class="line">CTRL-Z                susp     暂停当前命令</span><br><span class="line">DEL or CTRL-?         erase    删除最后一个字符</span><br><span class="line">BACKSPACE or CTRL-H	  erase    删除最后一个字符</span><br><span class="line">CTRL-U                kill     删除整行命令</span><br><span class="line">CTRL-D                eof      终止输入</span><br><span class="line">CTRL-S                stop     暂停输出</span><br><span class="line">CTRL-Q                start    恢复输出</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/3RJKVD3.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/3RJKVD3.html" class="post-title-link" itemprop="url">运维工具与常用命令</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-15 20:14:19" itemprop="dateCreated datePublished" datetime="2021-07-15T20:14:19+08:00">2021-07-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-07-16 22:11:52" itemprop="dateModified" datetime="2021-07-16T22:11:52+08:00">2021-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/3RJKVD3.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/3RJKVD3.html" class="cy_cmt_count" data-xid="2021/3RJKVD3.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="常用工具"><a href="#常用工具" class="headerlink" title="常用工具"></a>常用工具</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">vim文本文本编辑器</span></span><br><span class="line">yum install vim -y</span><br><span class="line"><span class="meta">#</span><span class="bash">lsof -i:3306 查看端口是否启用</span></span><br><span class="line">yum install lsof -y</span><br><span class="line"><span class="meta">#</span><span class="bash">当service报错时service: <span class="built_in">command</span> not found，使用服务必</span></span><br><span class="line">yum install initscripts -y</span><br></pre></td></tr></table></figure>

<h2 id="常用路径"><a href="#常用路径" class="headerlink" title="常用路径"></a>常用路径</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 修改系统配置</span></span><br><span class="line">vim /etc/profile</span><br></pre></td></tr></table></figure>

<h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">确认j启动</span></span><br><span class="line">ps -ef | grep minio | grep -v grep</span><br><span class="line"><span class="meta">#</span><span class="bash">数据库端口是否开启</span></span><br><span class="line">lsof -i:3306</span><br><span class="line"><span class="meta">#</span><span class="bash">查看d</span></span><br><span class="line">netstat -lnp|grep 6379</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">解压tar包</span></span><br><span class="line">tar -xvf  a.tar</span><br><span class="line"><span class="meta">#</span><span class="bash">安装</span></span><br><span class="line">rpm -ivh 包全名</span><br><span class="line"><span class="meta">#</span><span class="bash">j</span></span><br><span class="line">rpm -evh 包名</span><br></pre></td></tr></table></figure>

<h3 id="防火墙"><a href="#防火墙" class="headerlink" title="防火墙"></a>防火墙</h3><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">开放端口</span></span><br><span class="line">firewall-cmd --zone=public --add-port=6379/tcp --permanent</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">重启防火墙</span></span><br><span class="line">systemctl restart firewalld</span><br></pre></td></tr></table></figure>

<h3 id="启动命令"><a href="#启动命令" class="headerlink" title="启动命令"></a>启动命令</h3><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"><span class="comment">#systemctl操作</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">启动redis服务</span></span><br><span class="line">systemctl start redis</span><br><span class="line"><span class="meta">#</span><span class="bash">查看redis状态</span></span><br><span class="line">systemctl status redis</span><br><span class="line"><span class="meta">#</span><span class="bash">停止服务</span></span><br><span class="line">systemctl stop redis </span><br><span class="line"><span class="meta">#</span><span class="bash">重启服务</span></span><br><span class="line">systemctl restart redis</span><br><span class="line"><span class="meta">#</span><span class="bash">开机自动重启</span></span><br><span class="line">systemctl enable redis</span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment">#service操作</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">启动mysql</span></span><br><span class="line">service mysqld start</span><br><span class="line"><span class="meta">#</span><span class="bash">关闭mysql</span></span><br><span class="line">service mysqld stop</span><br></pre></td></tr></table></figure>

<h3 id="开机启动"><a href="#开机启动" class="headerlink" title="开机启动"></a>开机启动</h3><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">cd /etc/rc.d/</span><br><span class="line">chmod +x /etc/rc.d/rc.local</span><br><span class="line"></span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2021/2ARWSSS.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/2ARWSSS.html" class="post-title-link" itemprop="url">安装redis</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-07-15 20:14:14" itemprop="dateCreated datePublished" datetime="2021-07-15T20:14:14+08:00">2021-07-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/operations/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2021/2ARWSSS.html#SOHUCS" itemprop="discussionUrl">
        <span id="url::https://mixley.github.io/2021/2ARWSSS.html" class="cy_cmt_count" data-xid="2021/2ARWSSS.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">redis</span></span><br><span class="line">yum install redis -y</span><br></pre></td></tr></table></figure>

<h2 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">启动redis服务</span></span><br><span class="line">systemctl start redis</span><br><span class="line"><span class="meta">#</span><span class="bash">查看redis状态</span></span><br><span class="line">systemctl status redis</span><br><span class="line"><span class="meta">#</span><span class="bash">停止服务</span></span><br><span class="line">systemctl stop redis </span><br><span class="line"><span class="meta">#</span><span class="bash">重启服务</span></span><br><span class="line">systemctl restart redis</span><br><span class="line"><span class="meta">#</span><span class="bash">开机自动重启</span></span><br><span class="line">systemctl enable redis</span><br></pre></td></tr></table></figure>

<h2 id="其它命令"><a href="#其它命令" class="headerlink" title="其它命令"></a>其它命令</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">连接redis</span></span><br><span class="line">redis-cli -h 127.0.0.1 -p 6379</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">获取当前的db数</span> </span><br><span class="line">config get databases</span><br><span class="line"><span class="meta">#</span><span class="bash">切换指定db (可为0-15)</span></span><br><span class="line">select 1</span><br></pre></td></tr></table></figure>

<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">redis配置</span></span><br><span class="line">vim /etc/redis.conf</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment">#将下面注释，并设置protected-mode为no</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="built_in">bind</span> 127.0.0.1</span></span><br><span class="line"><span class="meta">#</span><span class="bash">保护模式</span></span><br><span class="line">protected-mode no</span><br><span class="line"><span class="meta">#</span><span class="bash">默认端口</span></span><br><span class="line">port 6379</span><br><span class="line"><span class="meta">#</span><span class="bash">设置密码</span></span><br><span class="line">requirepass 1111</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">需要重启</span></span><br><span class="line">systemctl restart redis</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/2/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><a class="page-number" href="/page/2/">2</a><span class="page-number current">3</span><a class="page-number" href="/page/4/">4</a><a class="page-number" href="/page/5/">5</a><a class="extend next" rel="next" href="/page/4/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">mixley</p>
  <div class="site-description" itemprop="description">全栈·架构设计·算法</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">mixley</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  <script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=cyvxnSprz"></script>


</body>
</html>
